bash 如何处理因grep命令而收到的每一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16317961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:38:30  来源:igfitidea点击:

How to process each line received as a result of grep command

bashshellgrep

提问by XYZ_Linux

I have a number of lines retrieved from a file after running the grepcommand as follows:

运行grep命令后,我从文件中检索了许多行,如下所示:

var=`grep xyz abc.txt`

Let's say I got 10 lines which consists of xyz as a result.

假设我得到了 10 行由 xyz 组成的结果。

Now I need to process each line I got as a result of the grep command. How do I proceed for this?

现在我需要处理我作为 grep 命令的结果得到的每一行。我该如何处理?

回答by Mat

One of the easy ways is not to store the output in a variable, but directly iterate over it with a while/read loop.

一种简单的方法是不将输出存储在变量中,而是直接使用 while/read 循环对其进行迭代。

Something like:

就像是:

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.

此方案有多种变化,具体取决于您所追求的内容。

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:

如果您需要更改循环内的变量(并使该更改在循环外可见),则可以使用fedorqui 的回答中所述的流程替换:

while read -r line ; do
    echo "Processing $line"
    # your code goes here
done < <(grep xyz abc.txt)

回答by fedorqui 'SO stop harming'

You can do the following while readloop, that will be fed by the result of the grepcommand using the so called process substitution:

您可以执行以下while read循环,该循环将由grep使用所谓的进程替换的命令结果提供:

while IFS= read -r result
do
    #whatever with value $result
done < <(grep "xyz" abc.txt)

This way, you don't have to store the result in a variable, but directly "inject" its output to the loop.

这样,您不必将结果存储在变量中,而是直接将其输出“注入”到循环中。



Note the usage of IFS=and read -raccording to the recommendations in BashFAQ/001: How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?:

注的用法IFS=,并read -r根据BashFAQ / 001的建议:我怎样才能读取一个文件(数据流,变量)行由行(和/或场逐场)?

The -r option to read prevents backslash interpretation (usually used as a backslash newline pair, to continue over multiple lines or to escape the delimiters). Without this option, any unescaped backslashes in the input will be discarded. You should almost always use the -r option with read.

In the scenario above IFS= prevents trimming of leading and trailing whitespace. Remove it if you want this effect.

read 的 -r 选项可防止反斜杠解释(通常用作反斜杠换行对,以继续多行或转义分隔符)。如果没有此选项,输入中任何未转义的反斜杠都将被丢弃。您几乎应该总是将 -r 选项与 read 一起使用。

在上面的场景中 IFS= 防止修剪前导和尾随空格。如果您想要这种效果,请将其删除。

Regarding the process substitution, it is explained in the bash hackers page:

关于进程替换,在bash 黑客页面中有解释:

Process substitution is a form of redirection where the input or output of a process (some sequence of commands) appear as a temporary file.

进程替换是一种重定向形式,其中进程的输入或输出(某些命令序列)作为临时文件出现。

回答by Julien Grenier

I would suggest using awk instead of grep + something else here.

我建议在这里使用 awk 而不是 grep + 其他东西。

awk '$0~/xyz/{ //your code goes here}' abc.txt

awk '$0~/xyz/{ //your code goes here}' abc.txt

回答by Nicolas Bonnici

Without any iteration with the --line-buffered grep option:

不使用 --line-buffered grep 选项进行任何迭代:

your_command | grep --line-buffered "your search"

Real life exemple with a Symfony PHP Framework router debug command ouput, to grep all "api" related routes:

使用 Symfony PHP 框架路由器调试命令输出的现实生活示例,用于 grep 所有与“api”相关的路由:

php bin/console d:r | grep --line-buffered "api"

回答by Abhi km

Iterate over the grep results with a while/read loop. Like:

使用 while/read 循环迭代 grep 结果。喜欢:

grep pattern filename.txt | while read -r line ; do
    echo "Matched Line:  $line"
    # your code goes here
done

回答by laurent

For those looking for a one-liner:

对于那些寻找单线的人:

grep xyz abc.txt | while read -r line; do echo "Processing $line"; done

回答by Ole Tange

Often the order of the processing does not matter. GNU Parallel is made for this situation:

通常处理的顺序无关紧要。GNU Parallel 是为这种情况而设计的:

grep xyz abc.txt | parallel echo do stuff to {}

If you processing is more like:

如果你的处理更像是:

grep xyz abc.txt | myprogram_reading_from_stdin

and myprogramis slow then you can run:

并且myprogram很慢然后你可以运行:

grep xyz abc.txt | parallel --pipe myprogram_reading_from_stdin