指令 diff 所產生的檔案來分析兩個檔案的不同,以 以行為單位做比較,比對方式是以ASCII 純文字檔為主 ,也可以針對兩個資料夾內的檔案做比較.
[root@localhost ~]# cat file1 # num1 add num2 example num1 = 1.5 num2 = 6.3 # Sum of num1 and num2 sum = float(num1) + float(num2) # Display the Result print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
[root@localhost ~]# cat file2 # num1 add num2 example num1 = 1.5 num2 = 7.7 # Sum of num1 and num2 sum = float(num1) + float(num2) # Display the Result print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
[root@localhost ~]# cat file3 # num1 add num2 example num1 = 2.5 num2 = 7.7 # num1 and num2 SUM sum = float(num1) + float(num2) print('The sum of {0} and {1} is {2}'.format(num1, num2, sum)) print('Testing1') print('Testing2')
[root@localhost ~]# diff file1 file2 3c3 < num2 = 6.3 --- > num2 = 7.7
顯示結果為 file1 file2 兩個檔案的差異.
- 3c3 代表 file1第三行 (c 表示 change) 與 file2 第三行不一樣 (其他符號 – a 表示 add, c 表示 change, d 表示 delete) ,接著後面會顯示前後檔案的差異之處.
[root@localhost ~]# diff file1 file3 2,4c2,4 < num1 = 1.5 < num2 = 6.3 < # Sum of num1 and num2 --- > num1 = 2.5 > num2 = 7.7 > # num1 and num2 SUM 6d5 < # Display the Result 7a7,8 > print('Testing1') > print('Testing2')
顯示結果為 file1 file3 兩個檔案的差異
- 2,4c2,4 代表 file1 第二行到第四行 (c 表示 change) 與 file3 第二行到第四行不一樣 ,後面會顯示前後檔案的不同之處.
- 6d5 代表 file1 第六行不存在 (d 表示 delete),依據 file3 第五行為基準,後面會顯示前後檔案的不同之處.
- 7a7,8 代表 file1 第七行為基準 (a 表示 add), file3 多了第七到第八行.,後面會顯示前後檔案的不同之處.
除了單獨兩個檔案比對外,我們還可以直接把兩個資料夾的內容一次比對完.
[root@localhost ~]# diff tempdir1/ tempdir2/ diff tempdir1/file1 tempdir2/file1 1c1 < # num1 add num2 example --- > ## num1 add num2 example diff tempdir1/file3 tempdir2/file3 8c8 < print('Testing2') --- > #print('Testing2')
常用參數:
- -y, –side-by-side , -W, –width=NUM
最方便人閱讀的方式,會把兩個檔案內容同時顯示出來(類似 #sdiff ),並指出差別在哪裡.
可以搭配參數 w 來指定輸出兩個欄位大小 ,預設為 130.> 表示後檔案比前檔案多了一行.
< 表示後檔案比前檔案少了一行.
! 表示兩者檔案差處.[root@localhost ~]# diff -y -W 80 file1 file3 # num1 add num2 example # num1 add num2 example num1 = 1.5 | num1 = 2.5 num2 = 6.3 | num2 = 7.7 # Sum of num1 and num2 | # num1 and num2 SUM sum = float(num1) + float(num2) sum = float(num1) + float(num2) # Display the Result < print('The sum of {0} and {1} is {2}' print('The sum of {0} and {1} is {2}' > print('Testing1') > print('Testing2')
- -c, -C NUM, –context[=NUM]
使用上下對比的比較方式,還多了比較檔案的說明以及行數,用來表示檔案內容的差異符號也不同.+ 同 a 表示 add ,表示後檔案比前檔案多了一行.
– 同 d 表示 delete ,表示後檔案比前檔案少了一行.
! 同 c 表示 change ,表示兩者檔案差處.[root@localhost ~]# diff -c file1 file3 *** file1 2019-07-12 15:29:38.984195404 +0800 --- file3 2019-07-12 15:59:00.918275364 +0800 *************** *** 1,7 **** # num1 add num2 example ! num1 = 1.5 ! num2 = 6.3 ! # Sum of num1 and num2 sum = float(num1) + float(num2) - # Display the Result print('The sum of {0} and {1} is {2}'.format(num1, num2, sum)) --- 1,8 ---- # num1 add num2 example ! num1 = 2.5 ! num2 = 7.7 ! # num1 and num2 SUM sum = float(num1) + float(num2) print('The sum of {0} and {1} is {2}'.format(num1, num2, sum)) + print('Testing1') + print('Testing2')
-c 預設把錯誤的前後 3 行的內容顯示出來,這時候可用 -C NUM 指定把有差異的前後 NUM 行顯示出來 (如 NUM =1 ,即把有差異的部分的前後一行都顯示出來)
[root@localhost ~]# diff -c file1 file2 *** file1 2019-07-12 15:29:38.984195404 +0800 --- file2 2019-07-12 15:29:49.836026058 +0800 *************** *** 1,6 **** # num1 add num2 example num1 = 1.5 ! num2 = 6.3 # Sum of num1 and num2 sum = float(num1) + float(num2) # Display the Result --- 1,6 ---- # num1 add num2 example num1 = 1.5 ! num2 = 7.7 # Sum of num1 and num2 sum = float(num1) + float(num2) # Display the Result
[root@localhost ~]# diff -C 1 file1 file2 *** file1 2019-07-12 15:29:38.984195404 +0800 --- file2 2019-07-12 15:29:49.836026058 +0800 *************** *** 2,4 **** num1 = 1.5 ! num2 = 6.3 # Sum of num1 and num2 --- 2,4 ---- num1 = 1.5 ! num2 = 7.7 # Sum of num1 and num2
- -u, -U NUM, –unified[=NUM]
對比會直接顯示在上下行,還多了比較檔案的說明以及行數,用來表示檔案內容的差異符號也不同.
+ 代表後檔案比前檔案多了一行.
– 代表後檔案比前檔案少了一行.
– 接著 + ,代表兩者檔案差處(同時多行不同時,會連續顯示 – – – 再 +++).
@@ -1,7 +1,8 @@ ,-1,7 代表前檔案從第一行顯示了 7 行, +1,8 代表後檔案從第一行顯示了 8 行.[root@localhost ~]# diff -u file1 file3 --- file1 2019-07-12 15:29:38.984195404 +0800 +++ file3 2019-07-12 15:59:00.918275364 +0800 @@ -1,7 +1,8 @@ # num1 add num2 example -num1 = 1.5 -num2 = 6.3 -# Sum of num1 and num2 +num1 = 2.5 +num2 = 7.7 +# num1 and num2 SUM sum = float(num1) + float(num2) -# Display the Result print('The sum of {0} and {1} is {2}'.format(num1, num2, sum)) +print('Testing1') +print('Testing2')
-u 預設把錯誤的前後 3 行的內容顯示出來,這時候可用 -U NUM 指定把有差異的前後 NUM 行顯示出來 (如 NUM =1 ,即把有差異的部分的前後一行都顯示出來)
[root@localhost ~]# diff -u file1 file2 --- file1 2019-07-12 15:29:38.984195404 +0800 +++ file2 2019-07-12 15:29:49.836026058 +0800 @@ -1,6 +1,6 @@ # num1 add num2 example num1 = 1.5 -num2 = 6.3 +num2 = 7.7 # Sum of num1 and num2 sum = float(num1) + float(num2) # Display the Result
[root@localhost ~]# diff -U 1 file1 file2 --- file1 2019-07-12 15:29:38.984195404 +0800 +++ file2 2019-07-12 15:29:49.836026058 +0800 @@ -2,3 +2,3 @@ num1 = 1.5 -num2 = 6.3 +num2 = 7.7 # Sum of num1 and num2
- -I, –ignore-matching-lines=RE
透過這個參數,可以讓輸出結果不顯示包含指定關鍵字的行.[root@localhost ~]# diff file1 file3 2,4c2,4 < num1 = 1.5 < num2 = 6.3 < # Sum of num1 and num2 --- > num1 = 2.5 > num2 = 7.7 > # num1 and num2 SUM 6d5 < # Display the Result 7a7,8 > print('Testing1') > print('Testing2')
不顯示包含有 num 這個關鍵字的行.
[root@localhost ~]# diff file1 file3 -I num 6d5 < # Display the Result 7a7,8 > print('Testing1') > print('Testing2')
-I 後面可以接 REGEXP (正規表示式),請參考 grep 正規表示式 https://benjr.tw/97395 說明.
- diff + sed
特殊用法,不比較內容有關於數字的部分,可以透過 sed 把數字刪除,然後再比較.[root@localhost ~]# diff file1 file3 2,4c2,4 < num1 = 1.5 < num2 = 6.3 < # Sum of num1 and num2 --- > num1 = 2.5 > num2 = 7.7 > # num1 and num2 SUM 6d5 < # Display the Result 7a7,8 > print('Testing1') > print('Testing2')
‘s/[0-9]//g’ – 刪除關於數字的內容.
[root@localhost ~]# diff <(sed 's/[0-9]//g' file1) <(sed 's/[0-9]//g' file3) 4c4 < # Sum of num and num --- > # num and num SUM 6d5 < # Display the Result 7a7,8 > print('Testing') > print('Testing')
更多關於 sed 的用法 s/regexp/replacement/ 請參考 – https://benjr.tw/97129
沒有解決問題,試試搜尋本站其他內容