Dejwi
Dejwi

Reputation: 4497

Bash while loop with read and IFS

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.

Upvotes: 2

Views: 2306

Answers (2)

chepner
chepner

Reputation: 532333

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

Upvotes: 5

anubhava
anubhava

Reputation: 786136

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)

Upvotes: 7

Related Questions