bash grep 使用另一个命令的输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23508923/
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-18 10:24:00  来源:igfitidea点击:

grep using output from another command

linuxbashunixgrep

提问by bruchowski

Say I have command1which outputs this:

说我有command1哪个输出:

b05808aa-c6ad-4d30-a334-198ff5726f7c
59996d37-9008-4b3b-ab22-340955cb6019
2b41f358-ff6d-418c-a0d3-ac7151c03b78
7ac4995c-ff2c-4717-a2ac-e6870a5670f0

I also have command2which outputs this:

我也有command2输出这个:

b05808aa-c6ad-4d30-a334-198ff5726f7c
59996d37-9008-4b3b-ab22-340955cb6019

Is there a way to grep the output from command1to not include any lines matched from command2, so that the final output would look like this?

有没有办法 grep 输出 fromcommand1不包括任何匹配 from 的行command2,以便最终输出看起来像这样?

2b41f358-ff6d-418c-a0d3-ac7151c03b78
7ac4995c-ff2c-4717-a2ac-e6870a5670f0

回答by Shiplu Mokaddim

Issue this grep

发出这个 grep

command1 | grep -vF -f <(command2)

Here,

这里,

-Fmeans Fixed string match*

-F表示固定字符串匹配*

-vmeans invert match

-v意味着反向匹配

-fmeans the file with patterns

-f表示带有模式的文件

<(command)actually creates a FIFO with that command and use it on redirection.

<(command)实际上使用该命令创建一个 FIFO 并在重定向时使用它。

回答by John1024

To get all the lines from the output of command1that do not appear in the output of command2:

要从 的输出中获取command1未出现在 的输出中的所有行command2

grep -vFf <(command2) <(command1)

-ftells grepto use patterns that come from a file. In this case, that file is the output of command2. -Ftells grepthat those patterns are to be treated as fixed strings, not regex. -vtells grepto invert its normal behavior and just show lines the lines that do notmatch.

-f告诉grep使用来自文件的模式。在这种情况下,该文件是command2. -F告诉grep这些模式将被视为固定字符串,而不是正则表达式。 -v告诉grep反转其正常行为,只显示匹配的行。