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 阅读(737) 评论(0)  编辑  收藏 所属分类: GNU牛力

只有注册用户登录后才能发表评论。


网站导航: