1. 程式人生 > >兩個文件內容差異對比,

兩個文件內容差異對比,

文件對比方法

比較兩個單行文件的方法就我知道的而言有4種

  1. 用diff

  2. 用grep

  3. 用comm

  4. 用uniq

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

1.用diff -c file1多的是"-"file2多的是"+"按這個就可以過濾出來了

[[email protected] ~]# 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,下面的輸出大家都可以看的很清楚了

[[email protected] ~]# grep -vwf file1 file2
6
7
[[email protected] ~]# grep -vwf file2 file1
1
[[email protected] ~]# 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

[[email protected] ~]# comm file1 file2
1
		2
		3
		4
		5
	6
	7
[[email protected] ~]# comm file1 file2 -13
6
7
[[email protected] ~]# comm file1 file2 -12
2
3
4
5
[[email protected] ~]# comm file1 file2 -23
1

comm命令會把文件分為三列,第一列是file1有而file2沒有的
第二列是file2有而file1沒有的
第三列是file1和file2沒有的共有的
分別對應1 2 3
-1是不顯示第一列
-2是不顯示第二列
-3是不顯示第三列

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

[[email protected] ~]# sort file1 file2|uniq
1
2
3
4
5
6
7
[[email protected] ~]# sort file1 file2|uniq -u
1
6
7
[[email protected] ~]# sort file1 file2|uniq -d
2
3
4
5

-u是差集-d是交集


本文出自 “Forand” 博客,請務必保留此出處http://853056088.blog.51cto.com/12966870/1959326

兩個文件內容差異對比,