linux系统下的comm命令是一个非常实用的文件对比命令,comm主要功能选择或拒绝两个已排序的文件的公共的行,下面为大家详细讲解一下Linux comm命令使用实例。

准备文件1.txt和2.txt
[h_chenliling@vm6-sj1-pro-had-32-107 ~]$ cat 1.txt
a
b
c
d
e
f1234567
[h_chenliling@vm6-sj1-pro-had-32-107 ~]$ cat 2.txt
d
e
f
g
h
i1234567
[h_chenliling@vm6-sj1-pro-had-32-107 ~]$ comm 1.txt 2.txt
a
b
c
d
e
f
g
h
i12345678910
第一列只包含在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
f1234
差集
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
c1234
求差
两个文件不相同的行,删除第3列,合并第1列和第2列
[h_chenliling@vm6-sj1-pro-had-32-107 ~]$ comm 1.txt 2.txt -3 | sed 's/^\t//'
a
b
c
g
h
i
以上就是良许教程网为各位朋友分享的Linu系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !
