使用读取和IFS进行Bash while循环 [英] Bash while loop with read and IFS
本文介绍了使用读取和IFS进行Bash while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须解析以下格式的文件:
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
因为整行都分配给了param1.
As the whole line is assigned to the param1.
推荐答案
您可以使用 过程替代 :
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)
这篇关于使用读取和IFS进行Bash while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文