統一格式是對上下文格式的修改版本,它不顯示重復的上下文而且還用其他辦法壓縮輸出內容.統一格式以下面的開頭來標識要比較大文件:
--- 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則忽略在原本沒有空白的地方添加的空白.