okokoklclclc的个人空间 https://blog.eetop.cn/289017 [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

diff命令的基本用法

已有 4990 次阅读| 2010-7-22 17:44

摘要:本文详细介绍了diff命令的基本用法

 作者:zieckey (zieckey@yahoo.com.cn)
    All Rights Reserved!

有这样两个文件:

程序清单1 :hello.c

#include <stdio.h>

int main(void)
{
    char msg[] = "Hello world!";
    
    puts(msg);
    printf("Welcome to use diff commond.\n");
    
    return 0;    
}


程序清单2:hello_diff.c

#include <stdio.h>
#include <stdlib.h>

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 <stdlib.h>
5c6
<       char msg[] = "Hello world!";
---
>       char msg[] = "Hello world,fome hello_diff.c";
8c9
<       printf("Welcome to use diff commond.\n");
---
>       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 <stdio.h>                                              #include <stdio.h>
                                                              > #include <stdlib.h>

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 <stdio.h>
  
  int main(void)
  {
!       char msg[] = "Hello world!";
  
        puts(msg);
!       printf("Welcome to use diff commond.\n");
  
        return 0;
  }
--- 1,12 ----
  #include <stdio.h>
+ #include <stdlib.h>
  
  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 <stdio.h>
+#include <stdlib.h>
 
 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”字样的行 

点赞

发表评论 评论 (1 个评论)

回复 zhouzhiping849 2010-7-26 21:11
:loveliness: 关于文件比较的部分,我比较建议用vim来做,具体用法:gvimdiff 1.v 2.v,vim会将分左右分别进行比较,相对来说我觉得会更加明了。

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 1

    粉丝
  • 0

    好友
  • 0

    获赞
  • 5

    评论
  • 899

    访问数
关闭

站长推荐 上一条 /1 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-5-3 07:07 , Processed in 0.027760 second(s), 14 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部