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.

Dejwi
  • 4,393
  • 12
  • 45
  • 74

2 Answers2

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)
anubhava
  • 761,203
  • 64
  • 569
  • 643
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
chepner
  • 497,756
  • 71
  • 530
  • 681