0

There are vaguely similar answers here but nothing that could really answer my question. I am at a point in my bash script where I have to fill two arrays from an output that looks like the following:

part-of-the-file1:line_32
part-of-the-file1:line_97
part-of-the-file2:line_88
part-of-the-file2:line_93

What I need to do is pull out the files and line numbers in there own separate arrays. So far I have:

read FILES LINES <<<($(echo $INPUTFILES | xargs grep -n $id | cut -f1,2 -d':' | awk '{print $1 " " $2}' with a modified IFS=':' but this doesn't work. I'm sure there are better solutions since I am NOT a scripting or tools wizard.

1 Answer 1

5

read cannot populate two arrays at a time like it can populate two regular parameters using word-splitting. It's best to just iterate over the output of your pipeline to build the arrays.

while IFS=: read fname lineno therest; do
  files+=("$fname")
  lines+=("$lineno")
done < <(echo $INPUTFILES | xargs grep -n "$id")

Here, I'm letting read do the splitting and discarding of the the tail end of grep's output in place of using cut and awk.

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

1 Comment

@jnbbender, if this is then answer, accept it

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.