Diff

-u of diff 到底有什麼作用?

  • April 26, 2013

看起來-u可以帶一個額外的數字參數,但我沒有安靜地拿到手冊,

它說,

 -u, -U NUM, --unified[=NUM]
         output NUM (default 3) lines of unified context

有人舉個例子嗎?

我找不到工作的。

-u 本身以“統一”格式輸出結果,該數字僅更改作為上下文包含的相鄰行數。

直接來自維基百科的文章diff

統一上下文差異最初是由 Wayne Davison 在 1990 年 8 月開發的(在 unidiff 中出現在 comp.sources.misc 的第 14 卷中)。一個月後,Richard Stallman 為 GNU 項目的 diff 實用程序添加了統一差異支持,並且該功能在 1991 年 1 月發布的 GNU diff 1.15 中首次亮相。GNU diff 已經將上下文格式推廣到允許任意格式化差異。

該格式以與上下文格式相同的兩行標題開頭,不同之處在於原始文件以“—”開頭,新文件以“+++”開頭。在此之後是一個或多個包含文件中行差異的更改塊。未更改的上下文行前面有一個空格字元,添加行前面有一個加號,刪除行前面有一個減號。

大塊以範圍資訊開始,緊隨其後的是行添加、行刪除和任意數量的上下文行。範圍資訊由雙 at 符號包圍,並將上下文格式(上圖)中出現在兩行上的內容組合成一行。範圍資訊行的格式如下:

@@ -l,s +l,s @@ optional section heading

然後在整個文件中要進行更改的地方,你會像這樣:

-check this dokument. On
+check this document. On

注意: A-表示它正在被刪除,a+表示它正在被添加。

該命令diff -U還可以採用一個附加參數,一個數字,表示在兩個文件之間出現差異的點周圍有多少行相鄰文本。這有助於更好地了解差異的實際含義。

例子

文件 1

$ cat file1.txt 
The Rain in Spain by
Servants Poor Professor Higgins!
Poor Professor Higgins! Night and day
He slaves away! Oh, poor Professor Higgins!
All day long On his feet; Up and down until he's numb;
Doesn't rest; Doesn't eat;

文件2

$ cat file2.txt 
The Rain in Spain by
added extra line here
Servants Poor Professor Higgins!
Poor Professor Higgins! Night and day
He slaves away! Oh, poor Professor Higgins!
All day long On his feat; Up and down untile he's numb;
Doesn't rest; Doesn't eat;

差異

$ diff -U 2 file1.txt file2.txt 
--- file1.txt   2013-04-26 09:39:13.496835363 -0400
+++ file2.txt   2013-04-26 09:38:04.838299195 -0400
@@ -1,6 +1,7 @@
The Rain in Spain by
+added extra line here
Servants Poor Professor Higgins!
Poor Professor Higgins! Night and day
He slaves away! Oh, poor Professor Higgins!
-All day long On his feet; Up and down until he's numb;
+All day long On his feat; Up and down untile he's numb;
Doesn't rest; Doesn't eat;

有關更多詳細資訊,請參見維基百科上的差異。

引用自:https://unix.stackexchange.com/questions/73771