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

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

看起來-u可以採取額外的數字參數,但我不安靜地得到手冊,

它說,

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

有人請舉例嗎?

我找不到一個可以工作的。

答案1

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

答案2

直接取自維基百科文章diff

統一上下文差異最初由 Wayne Davison 於 1990 年 8 月開發(unidiff 出現在 comp.sources.misc 第 14 卷中)。一個月後,Richard Stallman 在GNU 專案的diff 實用程式中添加了統一diff 支持,該功能在1991 年1 月發布的GNU diff 1.15 中首次亮相。任意格式設定。

該格式以與上下文格式相同的兩行標題開頭,只不過原始文件前面帶有“---”,新文件前面帶有“+++”。接下來是一個或多個包含文件中行差異的更改區塊。未更改的上下文行前面有一個空格字符,添加行前面有一個加號,刪除行前面有一個減號。

區塊以範圍資訊開始,緊隨其後的是行新增、行刪除和任意數量的上下文行。範圍資訊由雙 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;

維基百科上的差異更多細節。

相關內容