diff命令的使用

 

摘要:本文詳細介紹了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
&gt; #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>
                                                              &gt; #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”字樣的行

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章