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
差集
[h_chenliling@vm6-sj1-pro-had-32-107 ~]$ comm 1.txt 2.txt -2 -3
a
b
c
求差
[h_chenliling@vm6-sj1-pro-had-32-107 ~]$ comm 1.txt 2.txt -3 | sed 's/^\t//'