diff的输出格式分为传统格式和统一格式
1)diff的传统格式输出.
- suse:~/test/diff_patch # cat before.txt
- This is a line to be deleted
- This is a line that will be changed
- This is a line that will be unchanged
- suse:~/test/diff_patch # cat after.txt
- s is a line that has been changed
- This is a line that will be unchanged
- This is a line that has been added
- suse:~/test/diff_patch # diff after.txt before.txt
- 1c1,2
- < s is a line that has been changed
- ---
- > This is a line to be deleted
- > This is a line that will be changed
- 3d3
- < This is a line that has been added
- suse:~/test/diff_patch # diff before.txt after.txt
- 1,2c1
- < This is a line to be deleted
- < This is a line that will be changed
- ---
- > s is a line that has been changed
- 3a3
- > This is a line that has been added
注释:
传统格式的输出
1,2c1是指替换第1个文件的第1,2行到第2个文件的第1行,这里的1,2是指第1个文件的第1,2行,c是替换的意思,最后的1是第2个文件的第1行
<号是指第1个文件更改或删除的行
---号是分割两个文件
>号是第2个文件中增加或删除的行
3a3是指将第2个文件的第3行插入到第一个文件的第3行
- suse:~/test/diff_patch # diff before.txt after.txt >patch.txt
- suse:~/test/diff_patch # cat patch.txt
- 1,2c1
- < This is a line to be deleted
- < This is a line that will be changed
- ---
- > s is a line that has been changed
- 3a3
- > This is a line that has been added
- suse:~/test/diff_patch # cat patch.txt |patch before.txt
- patching file before.txt
- suse:~/test/diff_patch # diff before.txt after.txt
- suse:~/test/diff_patch # cmp before.txt after.txt
- suse:~/test/diff_patch # patch -R before.txt < patch.txt
- patching file before.txt
- suse:~/test/diff_patch # diff before.txt after.txt
- 1,2c1
- < This is a line to be deleted
- < This is a line that will be changed
- ---
- > s is a line that has been changed
- 3a3
- > This is a line that has been added
- suse:~/test/diff_patch # cmp before.txt after.txt
- before.txt after.txt differ: char 1, line 1
- suse:~/test/diff_patch #
注:-R标记告诉patch在反向上应用区别或者撤销patch.
- suse:~/test/diff_patch # diff -u before.txt after.txt |tee patchu.txt
- --- before.txt 2012-12-07 10:20:21.536037543 +0800
- +++ after.txt 2012-12-07 10:13:52.712264114 +0800
- @@ -1,3 +1,3 @@
- -This is a line to be deleted
- -This is a line that will be changed
- +s is a line that has been changed
- This is a line that will be unchanged
- +This is a line that has been added
- suse:~/test/diff_patch # mkdir old new
- suse:~/test/diff_patch # echo "This is one. It's unchanged." | tee old/one new/one
- This is one. It's unchanged.
- suse:~/test/diff_patch # echo "This is two. It will change." > old/two
- suse:~/test/diff_patch # echo "This is two. It changed.">new/two
- suse:~/test/diff_patch # echo "This is three. It's new" > new/three
- suse:~/test/diff_patch # diff -Nur old/ new/ >patchu.txt
- suse:~/test/diff_patch # more patchu.txt
- diff -Nur old/three new/three
- --- old/three 1970-01-01 08:00:00.000000000 +0800
- +++ new/three 2012-12-07 10:28:47.854496047 +0800
- @@ -0,0 +1 @@
- +This is three. It's new
- diff -Nur old/two new/two
- --- old/two 2012-12-07 10:28:45.884604177 +0800
- +++ new/two 2012-12-07 10:28:45.891603793 +0800
- @@ -1 +1 @@
- -This is two. It will change.
- +This is two. It changed.
- suse:~/test/diff_patch # patch --dir old< patchu.txt
- patching file three
- patching file two
- suse:~/test/diff_patch # ls -l old/
- total 12
- -rw-r--r-- 1 root root 29 Dec 7 10:28 one
- -rw-r--r-- 1 root root 24 Dec 7 10:30 three
- -rw-r--r-- 1 root root 25 Dec 7 10:30 two
- suse:~/test/diff_patch # patch --dir old -R < patchu.txt
- patching file three
- patching file two
- suse:~/test/diff_patch # ls -l old/
- total 8
- -rw-r--r-- 1 root root 29 Dec 7 10:28 one
- -rw-r--r-- 1 root root 29 Dec 7 10:30 two
- suse:~/test/diff_patch #
注释:
diff -u选项是统一格式输出.
--- before.txt 2009-06-20 05:21:49.000000000 +0800
--- before.txt是指旧文件
+++ after.txt 2009-06-20 04:03:16.000000000 +0800
+++ after.txt是指新文件.
@@ -1,3 +1,3 @@
@@ -1,3是指第1个文件一共有3行,+1,3 @@是指第2个文件一共有3行.
-This is a line to be deleted
-This is a line that will be changed
是被删除的行
+This is a line that has been changed
是增加的行
This is a line that will be unchanged
没有-号和+号是指该行不变,因为after.txt和before.txt都有这行.
+This is a line that has been added
是增加的行
diff的统一格式比较与输出是按顺序进行的.
阅读(2006) | 评论(0) | 转发(1) |