如何减去 B 中发生的 A 行?

How to subtract lines of A that happens in B?

我想使用前一个 pacman 命令的输出来过滤 pacman 命令的输出,并将结果发送到文件中,我的想法是去除第一个输出中的重复项在第二个中也存在。

假设:

a=`pacman -Qm`
# This produces the output
# package1 v0.0
# package2 v0.0
# packageN v0.0
b=`pacman -Qet`
# This produces the output
# package9 v0.0
# package2 v0.0
# packageN v0.0

我想要的是产生这个输出:

package1 v0.0

如果可以在文件中获取命令输出,则可以使用 comm 命令

来实现所需

comm - 逐行比较两个排序的文件

例如。以下

$pacman -Qm | sort > file1.txt
$pacman -Qet | sort > file2.txt

# in below command
# -2 suppress lines unique to file2.txt, 
# -3 supresses lines which appear in both files
# output will have only lines which are unique to file1.txt
$ comm -2 -3 file1.txt file2.txt > file3.txt 

使用差异http://linux.die.net/man/1/diff 将输出通过管道传输到 2 个文件。比较文件。