2

I have to parse a file in the following format:

line1_param1:line1_param2:line1_param3:
line1_param2:line2_param2:line2_param3:
line1_param3:line3_param2:line3_param3:

And process it line by line, extracting all parameters from current line. Currently I've managed it in such a way:

IFS=":"
grep "nice" some_file | sort > tmp_file
while read param1 param2 param3
do
  ..code here..
done < tmp_file
unset IFS

How can I avoid a creation of a temporary file?

Unfortunately this doesn't work correctly:

IFS=":"
while read param1 param2 param3
do
  ..code here..
done <<< $(grep "nice" some_file | sort)
unset IFS

As the whole line is assigned to the param1.

2 Answers 2

7

You can use process substitution for this:

while IFS=: read -r param1 param2 param3
do
   echo "Any code here to process: $param1 $param2 $param3"
done < <(grep "nice" some_file | sort)
Sign up to request clarification or add additional context in comments.

Comments

5

If you are using bash 4.2 or later, you can enable the lastpipe option to use the "natural" pipeline. The while loop will execute in the current shell, not a subshell, so and variables you set or change will still be visible following the pipeline. (Stealing code from anubhava's fine answer to demonstrate.)

shopt -s lastpipe
grep "nice" some_file | sort | while IFS=: read -r param1 param2 param3
do
   echo "Any code here to process: $param1 $param2 $param3"
done

Comments

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.