ivaneeo's blog

          自由的力量,自由的生活。

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks
          統一格式是對上下文格式的修改版本,它不顯示重復的上下文而且還用其他辦法壓縮輸出內容.統一格式以下面的開頭來標識要比較大文件:
            --- srcfile srcfile_timestamp
            其后是一個或多個塊(hunk),格式如下:
              @@ srcfile_range dstfile_range @@
                line_from_either_file
                line_from_either_file
            以@@開頭的每一行都標志一個塊的開始.在塊中,上下文行以空格開頭,而有差別的行以"+"或"-"開頭,以表示相對于srcfile在此位置上添加或刪除一行.
          命令diff -u hello.c howdy.c產生的輸出如下:
            ---hello.c  Web Aug 9 21:02:42 2000
            +++howdy.c  Web Aug 9 21:04:30 2000
            @@ -1,12 +1,13 @@
              #include <stdio.h>
              +#include <stdlib.h>

              int main(void)
              {
             -     char msg [] = "Hello,Linux programmer!";
             +     char msg [] = "Hello,Linux programmer, from howdy.c!";
            
                   puts(msg);
             -     printf("Here you are, using diff.\n");
             +     printf("howdy.c says, `Here you are, using diff.`\n");
             -     return 0;
             +     exit(EXIT_SUCCESS);
              }
          對這一輸出進行翻譯,用語言來描述怎么把hello.c轉變成howdy.c:
            .緊挨著#include <stdio.h>一行之后加入#include <stdlib.h>
            .緊挨著前半個大括號之后,刪除
              char msg[] = "Hello, Linux Programmer!";
              并加入
              char msg[] = "Hello, Linux Programmer, from howdy.c!";
            .緊挨著puts(msg);之后,刪除
              printf("Here you are, using diff.\n");
              并加入
              printf("howdy.c says, 'Here you are, using diff.'\n");
            .緊挨著最后一個空行之后,刪除
              return 0;
              并加入
              exit(EXIT_SUCCESS);
          雖然統一格式既緊湊又容易閱讀,但是統一格式的差異文件卻有一個缺點:目前,只有GNU diff能產生統一格式的差異文件而且只有GNU patch能理解統一格式.

          要判別兩個二進制文件的差異,只需把它們的名字作為參數傳遞給diff:
            $diff hello howdy
            Binary files hello and howdy differ
          如果要顯示有差別的行,則使用-a選項.但是需注意這樣做會輸出重定向到一個文件:
            $diff -a hello howdy > diffs
            Files hello.c and howdy.c differ
          要查看兩個文本文件是否不同但又不顯示差異之處,可以使用diff的-q選項:
            $diff -q hello.c howdy.c
            Files hello.c and howdy.c differ

            假如你想忽略某種差別.實現這個目的的做法是使用-I regexp選項.
              $diff -u -I include hello.c howdy.c
              --- hello.c Web Aug 9 21:02:42 2000
              +++ howdy.c Web Aug 9 21:04:30 2000
              @@ -2,11 +3,11 @@

              int main(void)
              {
             -       char msg[ ] = "Hello. Linux programmer!";
             +       char msg[ ] = "Hello. Linux programmer, from howdy.c!";

                     puts(msg);
              -      printf("Here you are, using diff.\n");
              +      printf("howdy.c says, `Here you are, using diff.\n'";
              -      return 0;
              +      exit(EXIT_SUCCESS);
              }
          上面的例子使用了-I regexp來忽略"include"的行.

          另一組有用的diff命令行選項能改變它對空白的處理方式.-b選項讓diff忽略輸入文件中空白數量的變化;-B讓diff忽略刪除或插入空行的改動;-w在逐行比較時忽略空白的變化.-b和-w的區別在哪里?-b忽略的是輸入文件之間空白數量上的變化,而-w則忽略在原本沒有空白的地方添加的空白.
          posted on 2005-08-04 17:20 ivaneeo 閱讀(786) 評論(0)  編輯  收藏 所屬分類: GNU牛力
          主站蜘蛛池模板: 苍山县| 沈丘县| 定州市| 博兴县| 阿勒泰市| 汕尾市| 高平市| 平阴县| 乐山市| 革吉县| 南投县| 胶南市| 宜丰县| 柳江县| 呼图壁县| 抚松县| 利津县| 乐平市| 博野县| 师宗县| 乐亭县| 馆陶县| 福泉市| 来宾市| 邓州市| 土默特左旗| 菏泽市| 咸阳市| 万安县| 神木县| 左权县| 金沙县| 乌兰察布市| 万州区| 子长县| 奇台县| 突泉县| 益阳市| 扶沟县| 东安县| 天门市|