如何处理接收到的作为grep命令的结果的每一行 [英] how to process each line received as a result of grep command
问题描述
我没有。运行grep命令如下后,从文件中检索行
I have no. of lines retrieved from a file after running grep command as follow
var=`grep xyz abc.txt`
可以说我得到了10没有。行它由XYZ作为结果
lets say I got 10 no. of lines which consists xyz as a result.
现在我需要处理的每一行我是grep命令的结果。
如何进行此,请给一些想法..
Now I need to process each line I got as result of grep command. how to proceed for this , please give some idea ..
推荐答案
其中一个简单的方法是不是存储在一个变量的输出,而是直接遍历它一段时间/读循环。
One of the easy ways is not to store the output in a variable, but directly iterate over it with a while/read loop.
是这样的:
grep xyz abc.txt | while read -r line ; do
echo "Processing $line"
# your code goes here
done
有这取决于你到底是什么后该方案的变化。
There are variations on this scheme depending on exactly what you're after.
如果你需要改变的变量循环内(且这种变化是它的外部可见的),则可以使用替代过程中的 fedorqui的回答:
If you need to change variables inside the loop (and have that change be visible outside of it), you can use process substitution as stated in fedorqui's answer:
while read -r line ; do
echo "Processing $line"
# your code goes here
done < <(grep xyz abc.txt)
这篇关于如何处理接收到的作为grep命令的结果的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!