使用 diff 查找两个 grep 命令输出中的差异

使用 diff 查找两个 grep 命令输出中的差异

是否可以diff输出两个grep命令?

我目前正在使用不同的排除模式搜索文件,并且输出很长,所以我想看看我的新模式是否有效,或者输出是否仍然相同。

是否有可能以某种方式将两个grep命令导入到管道中diff或类似的东西中?

grep --exclude=*{.dll, pdb} --ril "dql"
grep  --ril "dql"

答案1

使用 飞行中

diff <(grep pattern file) <(grep another_pattern file)

进程替换:<(command)>(command)由 FIFO 或 /dev/fd/* 条目替换。基本上是设置命名管道的简写。请参阅http://mywiki.wooledge.org/ProcessSubstitution
例子:diff -u <(sort file1) <(sort file2)

所以 :

diff <(grep --exclude=*{.dll, pdb} --ril "dql") <(grep  --ril "dql")

答案2

bash使用流程替代

$ echo a > FILE
$ echo b > FILE1
$ diff <(grep a *) <(grep b *)
1c1
< FILE:a
---
> FILE1:b

如下所述man bash

   Process Substitution
   Process substitution is supported on systems that support named
   pipes (FIFOs) or the /dev/fd method of naming open files.  It
   takes the form of <(list) or >(list).  The process list is run
   with its input or output connected to a FIFO or some file in
   /dev/fd.  The name of this file is passed as an argument to the
   current command as the result of the expansion.  If the >(list)
   form is used, writing to the file will provide input for list.
   If the <(list) form is used, the file passed as an argument
   should be read to obtain the output of list.

   When available, process substitution is performed
   simultaneously with parameter and variable expansion,
   command substitution, and arithmetic expansion.

相关内容