linux shell 查找文件差異並進行修補

例:
[root@localhost test]# cat A.txt 
1
2
5
[root@localhost test]# cat B.txt 
1
3
6

[root@localhost test]# diff A.txt B.txt -u
--- A.txt       2012-05-19 11:09:01.000000000 +0800
+++ B.txt       2012-05-19 11:09:25.000000000 +0800
@@ -1,3 +1,3 @@
 1
-2
-5
+3
+6
其中加上-u是生成一體化輸出,這樣易讀性更好,更易於看出兩個文件之間的差異。
在一體化的diff中,以+起始的行表示是新加入的行,-起始的行表示被刪除的行。
將一體化的修補文件,重定向到一個新文件,得到修補文件:
[root@localhost test]# diff A.txt B.txt -u > version.patch
將修補誰的用於A.txt,會將A.txt修改爲B.txt:
[root@localhost test]# patch -p1 A.txt < version.patch 
missing header for unified diff at line 3 of patch
patching file A.txt
[root@localhost test]# cat A.txt 
1
3
6
同樣,將修被文件用於B.txt,會將B.txt修改爲A.txt:
[root@localhost test]# patch -p1 B.txt < version.patch 
missing header for unified diff at line 3 of patch
patching file B.txt
Reversed (or previously applied) patch detected!  Assume -R? [n] y
[root@localhost test]# cat B.txt
1
2
5
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章