grep使用另一个命令的输出

时间:2022-11-21 13:56:00

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?

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

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

2 个解决方案

#1


15  

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.

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

#2


5  

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 not match.

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

#1


15  

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.

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

#2


5  

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 not match.

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