Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1308605
  • 博文数量: 273
  • 博客积分: 5865
  • 博客等级: 准将
  • 技术积分: 3280
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-11 10:01
文章分类

全部博文(273)

文章存档

2015年(33)

2014年(11)

2013年(11)

2012年(136)

2011年(32)

2010年(50)

分类: LINUX

2012-12-07 10:24:00

diff的输出格式分为传统格式和统一格式


1)diff的传统格式输出.

点击(此处)折叠或打开

  1. suse:~/test/diff_patch # cat before.txt
  2. This is a line to be deleted
  3. This is a line that will be changed
  4. This is a line that will be unchanged

  5. suse:~/test/diff_patch # cat after.txt
  6. s is a line that has been changed
  7. This is a line that will be unchanged
  8. This is a line that has been added
  9. suse:~/test/diff_patch # diff after.txt before.txt
  10. 1c1,2
  11. < s is a line that has been changed
  12. ---
  13. > This is a line to be deleted
  14. > This is a line that will be changed
  15. 3d3
  16. < This is a line that has been added
  17. suse:~/test/diff_patch # diff before.txt after.txt
  18. 1,2c1
  19. < This is a line to be deleted
  20. < This is a line that will be changed
  21. ---
  22. > s is a line that has been changed
  23. 3a3
  24. > 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行

点击(此处)折叠或打开

  1. suse:~/test/diff_patch # diff before.txt after.txt >patch.txt
  2. suse:~/test/diff_patch # cat patch.txt
  3. 1,2c1
  4. < This is a line to be deleted
  5. < This is a line that will be changed
  6. ---
  7. > s is a line that has been changed
  8. 3a3
  9. > This is a line that has been added
  10. suse:~/test/diff_patch # cat patch.txt |patch before.txt
  11. patching file before.txt
  12. suse:~/test/diff_patch # diff before.txt after.txt
  13. suse:~/test/diff_patch # cmp before.txt after.txt
  14. suse:~/test/diff_patch # patch -R before.txt < patch.txt
  15. patching file before.txt
  16. suse:~/test/diff_patch # diff before.txt after.txt
  17. 1,2c1
  18. < This is a line to be deleted
  19. < This is a line that will be changed
  20. ---
  21. > s is a line that has been changed
  22. 3a3
  23. > This is a line that has been added
  24. suse:~/test/diff_patch # cmp before.txt after.txt
  25. before.txt after.txt differ: char 1, line 1
  26. suse:~/test/diff_patch #
注:-R标记告诉patch在反向上应用区别或者撤销patch.

点击(此处)折叠或打开

  1. suse:~/test/diff_patch # diff -u before.txt after.txt |tee patchu.txt
  2. --- before.txt 2012-12-07 10:20:21.536037543 +0800
  3. +++ after.txt 2012-12-07 10:13:52.712264114 +0800
  4. @@ -1,3 +1,3 @@
  5. -This is a line to be deleted
  6. -This is a line that will be changed
  7. +s is a line that has been changed
  8. This is a line that will be unchanged
  9. +This is a line that has been added
  10. suse:~/test/diff_patch # mkdir old new
  11. suse:~/test/diff_patch # echo "This is one. It's unchanged." | tee old/one new/one
  12. This is one. It's unchanged.
  13. suse:~/test/diff_patch # echo "This is two. It will change." > old/two
  14. suse:~/test/diff_patch # echo "This is two. It changed.">new/two
  15. suse:~/test/diff_patch # echo "This is three. It's new" > new/three
  16. suse:~/test/diff_patch # diff -Nur old/ new/ >patchu.txt
  17. suse:~/test/diff_patch # more patchu.txt
  18. diff -Nur old/three new/three
  19. --- old/three 1970-01-01 08:00:00.000000000 +0800
  20. +++ new/three 2012-12-07 10:28:47.854496047 +0800
  21. @@ -0,0 +1 @@
  22. +This is three. It's new
  23. diff -Nur old/two new/two
  24. --- old/two 2012-12-07 10:28:45.884604177 +0800
  25. +++ new/two 2012-12-07 10:28:45.891603793 +0800
  26. @@ -1 +1 @@
  27. -This is two. It will change.
  28. +This is two. It changed.
  29. suse:~/test/diff_patch # patch --dir old< patchu.txt
  30. patching file three
  31. patching file two
  32. suse:~/test/diff_patch # ls -l old/
  33. total 12
  34. -rw-r--r-- 1 root root 29 Dec 7 10:28 one
  35. -rw-r--r-- 1 root root 24 Dec 7 10:30 three
  36. -rw-r--r-- 1 root root 25 Dec 7 10:30 two
  37. suse:~/test/diff_patch # patch --dir old -R < patchu.txt
  38. patching file three
  39. patching file two
  40. suse:~/test/diff_patch # ls -l old/
  41. total 8
  42. -rw-r--r-- 1 root root 29 Dec 7 10:28 one
  43. -rw-r--r-- 1 root root 29 Dec 7 10:30 two
  44. 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的统一格式比较与输出是按顺序进行的.
阅读(1972) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~