比较两个单行文件的方法就我知道的而言有4种

  1. 用diff

  2. 用grep

  3. 用comm

  4. 用uniq

[root@hxy ~]# echo "`seq 5`" >file1;cat file1
1
2
3
4
5
[root@hxy ~]# echo "`seq 2 7`" >file2;cat file2
2
3
4
5
6
7

1.用diff -c file1多的是"-"file2多的是"+"按这个就可以过滤出来了

[root@hxy ~]# diff -c file1  file2
*** file1	2017-08-25 15:04:58.180986783 +0800
--- file2	2017-08-25 15:05:07.805865181 +0800
***************
*** 1,5 ****
- 1
  2
  3
  4
  5
--- 1,6 ----
  2
  3
  4
  5
+ 6
+ 7

2.用grep -vwf,下面的输出大家都可以看的很清楚了

[root@hxy ~]# grep -vwf file1 file2
6
7
[root@hxy ~]# grep -vwf file2 file1
1
[root@hxy ~]# grep -wf file2 file1
2
3
4
5

grep -vwf file1 file2 #输出文件1没有而文件2有的
grep -vwf file2 file1 #输出文件2没有而文件1有的
grep -wf file2 file1 输入他们的交集

3.用comm

[root@hxy ~]# comm file1 file2
1
		2
		3
		4
		5
	6
	7
[root@hxy ~]# comm file1 file2 -13
6
7
[root@hxy ~]# comm file1 file2 -12
2
3
4
5
[root@hxy ~]# comm file1 file2 -23
1

comm命令会把文件分为三列,第一列是file1有而file2没有的
第二列是file2有而file1没有的
第三列是file1和file2没有的共有的
分别对应1 2 3
-1是不显示第一列
-2是不显示第二列
-3是不显示第三列

4.用uniq 就只能做交集和差集的比对

[root@hxy ~]# sort file1 file2|uniq
1
2
3
4
5
6
7
[root@hxy ~]# sort file1 file2|uniq -u
1
6
7
[root@hxy ~]# sort file1 file2|uniq -d
2
3
4
5

-u是差集-d是交集