comm 命令
能够用于两个文件之间的比较,它有一些选项能够用来调整输出,以便执行交集、求差、差集操做。ide
交集:打印两个文件所共有的行code
求差:打印出指定文件所包含的其不相同的行。it
差集:打印出包含在一个文件中,但不包含在其余指定文件中的行。class
comm(选项)(参数)
-1 :不显示在第一个文件出现的内容;sed
-2 :不显示在第二个文件中出现的内容;file
-3 :不显示同时在两个文件中都出现的内容。语法
文件1 :指定要比较的第一个有序文件sort
文件2 :指定要比较的第二个有序文件di
[root@ceshi comm]# cat aaa.txt aaa bbb ccc ddd eee 111 222 [root@ceshi comm]# cat bbb.txt bbb ccc aaa hhh ttt jjj
[root@ceshi comm]# comm aaa.txt bbb.txt aaa bbb ccc comm: file 2 is not in sorted order aaa ddd eee comm: file 1 is not in sorted order 111 222 hhh ttt jjj
输出第一列:表明aaa.txt包含的内容文件
输出第二列:表明bbb.txt包含的内容
输出第三列:表明在aaa.txt和bbb.txt中相同的行。各列是以制表符(\t)做为定界符。
file 1 is not in sorted order :意思是文件里边的内容不是按顺序排列的。没有用关系。
交集:
打印两个文件的交集,须要删除第一列和第二列:
[root@ceshi comm]# comm aaa.txt bbb.txt -1 -2 bbb ccc
求差:
打印出两个文件中不相同的行,须要删除第三列:
[root@ceshi comm]# comm aaa.txt bbb.txt -3 aaa aaa ddd eee 111 222 hhh ttt jjj
[root@ceshi comm]# comm aaa.txt bbb.txt -3 | sed 's/^\t//' comm: file 2 is not in sorted order comm: file 1 is not in sorted order aaa aaa ddd eee 111 222 hhh ttt jjj
sed ‘s/^\t//’ 是将制表符(\t)删除,以便把两列合并成一列。
差集:
经过删除不须要的列,能够获得aaa.txt和bbb.txt的差集:
aaa.txt的差集
[root@ceshi comm]# comm aaa.txt bbb.txt -2 -3 aaa ddd eee 111 222
bbb.txt的差集
[root@ceshi comm]# comm aaa.txt bbb.txt -1 -3 aaa hhh ttt jjj