Bash while loop with read and IFS

人走茶凉 提交于 2020-02-06 01:33:51

问题


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.


回答1:


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)



回答2:


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


来源:https://stackoverflow.com/questions/25638795/bash-while-loop-with-read-and-ifs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!