0

I have strings coming in from the user that are of the form... 1,2,3 with any amount of numbers. What I want to do is replace the numbers with the corresponding values at the indeces of each of these numbers from an array. So say my array has [tom][jerry][sue]. I'd want my string to look like tom,jerry,sue when I'm doing processing it. Should I be using grep? bash?

1 Answer 1

5
string="1,2,3"
names=( nobody tom jerry sue )
indexes=( $(sed 's/,/ /g' <<< "$string") )
result=""
pad=""
for i in ${indexes[@]}
do
    result="$result$pad${names[$i]}"
    pad=","
done
echo "$result"

Explanation:

  1. Simulate input string from user
  2. Array of names, indexed from 0 (nobody) to 3 (sue)
  3. Array of numbers generated from string by requesting sed to replace commas by blanks. There are other ways to do that, but I tend to use sed still. For example, indexes=( ${string//,/ } will do the job without invoking sed or any external program.
  4. The output goes in result.
  5. I use pad to build up strings with a separator.
  6. Loop over the numbers in the array indexes
  7. Result is the concatenation of what was there before, the padding, and the name represented by the number in $i.
  8. Set the pad to a comma for the second and subsequent iterations.
  9. Echo the result.

I didn't count the 'do' or 'done' lines.


I should've been more specific and asked you how the sed syntax works.

There are 4 interesting parts to the line:

indexes=( $(sed 's/,/ /g' <<< "$string") )
  1. indexes=( ... )

    This is an array assignment. It will take the words produce by whatever is in ... and treat them as the elements of the array. That's how the names=( ... ) assignment works, of course.

  2. sed ... <<< "$string"

    This runs the sed command with the value of "$string" as its standard input. This is a bash extension called a Here String. Thus the only line of input is "1,2,3".

  3. 's/,/ /g' is a sed command that substitutes a space for every comma in the input. Thus, the output is a line containing 1 2 3.

  4. $( ... ) runs the command contained within the parentheses (the sed command) and treats the output as a string.

The overall effect is the same as:

indexes=( 1 2 3 )

The spaces around the parentheses aren't strictly necessary, but I think they make the array assignment easier to spot — but it is a notation I'm still 'learning' (having to remember to use).

Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain this?
I should've been more specific and asked you how the sed syntax works, but I can figure it out from what you've given me. Thanks.
No need for sed here: indexes=( $(sed 's/,/ /g' <<< "$string") ) can be replaced with IFS=, read -a indexes <<< "$string".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.