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
grep using output from another command
提问by bruchowski
Say I have command1
which 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 command2
which outputs this:
我也有command2
输出这个:
b05808aa-c6ad-4d30-a334-198ff5726f7c
59996d37-9008-4b3b-ab22-340955cb6019
Is there a way to grep the output from command1
to 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,
这里,
-F
means Fixed string match*
-F
表示固定字符串匹配*
-v
means invert match
-v
意味着反向匹配
-f
means 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 command1
that do not appear in the output of command2
:
要从 的输出中获取command1
未出现在 的输出中的所有行command2
:
grep -vFf <(command2) <(command1)
-f
tells grep
to use patterns that come from a file. In this case, that file is the output of command2
. -F
tells grep
that those patterns are to be treated as fixed strings, not regex. -v
tells grep
to invert its normal behavior and just show lines the lines that do notmatch.
-f
告诉grep
使用来自文件的模式。在这种情况下,该文件是command2
. -F
告诉grep
这些模式将被视为固定字符串,而不是正则表达式。 -v
告诉grep
反转其正常行为,只显示不匹配的行。