是否可以同時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.