comm命令可以用于两个文件之间的比较,前提是两个文件必须有序。
准备文件1.txt和2.txt

[h_chenliling@vm6-sj1-pro-had-32-107 ~]$ cat 1.txt
a
b
c
d
e
f
[h_chenliling@vm6-sj1-pro-had-32-107 ~]$ cat 2.txt 
d
e
f
g
h
i
[h_chenliling@vm6-sj1-pro-had-32-107 ~]$ comm 1.txt 2.txt
a
b
c
d
e
f
g
h
i

第一列只包含在1.txt中出现的行,第二列包含在2.txt中出现的行,第三列包含在1.txt和2.txt中相同的行。各列是以制表符(\t)作为定界符。

交集

删除第1列和第2列

[h_chenliling@vm6-sj1-pro-had-32-107 ~]$ comm 1.txt 2.txt  -1 -2
d
e
f

差集

1.txt的差集,1.txt存在,而2.txt中不存在。也就是删除第2列和第3列

[h_chenliling@vm6-sj1-pro-had-32-107 ~]$ comm 1.txt 2.txt  -2 -3
a
b
c

求差

两个文件不相同的行,删除第3列,合并第1列和第2列

[h_chenliling@vm6-sj1-pro-had-32-107 ~]$ comm 1.txt 2.txt  -3 | sed 's/^\t//'