免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 838 | 回复: 0
打印 上一主题 下一主题

diff命令使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-16 15:37 |只看该作者 |倒序浏览
目录对比:
diff -r -y ./extman/ extman-0.2.5/ >>extman.txt

并排输出各个文件的不同之处,并写入extman.txt


可参考下面文章:
有这样两个文件:
程序清单1 :hello.c
#include
int main(void)
{
    char msg[] = "Hello world!";
   
    puts(msg);
    printf("Welcome to use diff commond.\n");
   
    return 0;   
}
程序清单2:hello_diff.c
#include
#include
int main(void)
{
    char msg[] = "Hello world,fome hello_diff.c";
   
    puts(msg);
    printf("hello_diff.c says,'Here you are,using diff.'\n");
   
    return 0;   
}
我们使用diff命令来查看这两个文件的不同之处,有一下几种方便的方法:
1、普通格式输出:
[root@localhost diff]# diff hello.c hello_diff.c
1a2
> #include
5c6
       char msg[] = "Hello world,fome hello_diff.c";
8c9
       printf("hello_diff.c says,'Here you are,using diff.'\n");
[root@localhost diff]#
上面的“1a2”表示后面的一个文件"hello_diff.c"比前面的一个文件"hello.c"多了一行
"5c6"表示第一个文件的第5行与第二个文件的第6行有区别
2、并排格式输出
[root@localhost diff]# diff hello.c hello_diff.c -y -W 130
#include                                               #include
                                                              > #include
int main(void)                                                  int main(void)
{                                                               {
        char msg[] = "Hello world!";                          |         char msg[] = "Hello world,fome hello_diff.c";
        puts(msg);                                                      puts(msg);
        printf("Welcome to use diff commond.\n");             |         printf("hello_diff.c says,'Here you are,using diff.'\
        return 0;                                                       return 0;
}                                                               }
[root@localhost diff]#
这种并排格式的对比一目了然,可以快速找到不同的地方。
-W选择可以指定输出列的宽度,这里指定输出列宽为130
3、上下文输出格式
[root@localhost diff]# diff hello.c hello_diff.c -c
*** hello.c     2007-09-25 17:54:51.000000000 +0800
--- hello_diff.c        2007-09-25 17:56:00.000000000 +0800
***************
*** 1,11 ****
  #include
  
  int main(void)
  {
!       char msg[] = "Hello world!";
  
        puts(msg);
!       printf("Welcome to use diff commond.\n");
  
        return 0;
  }
--- 1,12 ----
  #include
+ #include
  
  int main(void)
  {
!       char msg[] = "Hello world,fome hello_diff.c";
  
        puts(msg);
!       printf("hello_diff.c says,'Here you are,using diff.'\n");
  
        return 0;
  }
[root@localhost diff]#
这种方式在开头两行作了比较文件的说明,这里有三中特殊字符:
+        比较的文件的后者比前着多一行
-        比较的文件的后者比前着少一行        
!        比较的文件两者有差别的行
4、统一输出格式
[root@localhost diff]# diff hello.c hello_diff.c -u
--- hello.c     2007-09-25 17:54:51.000000000 +0800
+++ hello_diff.c        2007-09-25 17:56:00.000000000 +0800
@@ -1,11 +1,12 @@
#include
+#include

int main(void)
{
-       char msg[] = "Hello world!";
+       char msg[] = "Hello world,fome hello_diff.c";

        puts(msg);
-       printf("Welcome to use diff commond.\n");
+       printf("hello_diff.c says,'Here you are,using diff.'\n");

        return 0;
}
[root@localhost diff]#
正如看到的那样,统一格式的输出更加紧凑,所以更易于理解,更易于修改。
5、其他
假如你想查看两个文件是否不同又不想显示差异之处的话,可以加上-q选项:
[root@localhost diff]# diff hello.c hello_diff.c -q
Files hello.c and hello_diff.c differ
[root@localhost diff]# 另外你还可以提供一些匹配规则来忽略某中差别,可以用 -I regexp
[root@localhost diff]# diff hello.c hello_diff.c -c -I include
*** hello.c     2007-09-25 17:54:51.000000000 +0800
--- hello_diff.c        2007-09-25 17:56:00.000000000 +0800
***************
*** 2,11 ****
  
  int main(void)
  {
!       char msg[] = "Hello world!";
  
        puts(msg);
!       printf("Welcome to use diff commond.\n");
  
        return 0;
  }
--- 3,12 ----
  
  int main(void)
  {
!       char msg[] = "Hello world,fome hello_diff.c";
  
        puts(msg);
!       printf("hello_diff.c says,'Here you are,using diff.'\n");
  
        return 0;
  }
[root@localhost diff]#
这里通过“ -I include”选项来忽略带有“ include”字样的行


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/8985/showart_1308956.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP