了解 diff 输出

了解 diff 输出

我有file1.txt

this is the original text  
line2  
line3  
line4  
happy hacking !  

file2.txt

this is the original text  
line2  
line4  
happy hacking !  
GNU is not UNIX  

如果我这样做:diff file1.txt file2.txt我得到:

3d2  
< line3  
5a5  
> GNU is not UNIX  

通常如何解释输出?我认为这<意味着删除,但是是什么3d2意思5a5

如果我做:

$ diff -u file1.txt file2.txt  
--- file1.txt        2013-07-06 17:44:59.180000000 +0200  
+++ file2.txt        2013-07-06 17:39:53.433000000 +0200  
@@ -1,5 +1,5 @@  
 this is the original text  
 line2  
-line3  
 line4  
 happy hacking !  
+GNU is not UNIX  

结果更清楚了,但是什么@@ -1,5 +1,5 @@意思呢?

答案1

在您的第一个diff输出(所谓的“正常差异”)中,含义如下:

<- 表示 file1.txt 中的行

>- 表示 file2.txt 中的行

3d25a5表示受影响的行号以及执行了哪些操作。d代表删除,a代表添加(c代表更改)。字符左边的数字是file1.txt 中的行号,右边的数字是file2.txt 中的行号。因此3d2告诉您 file1.txt 中的第 3 行已被删除,并且 file2.txt 中的行号为 2(或者更好的说法是,删除后行计数器返回到行号 2)。5a5告诉您我们从 file1.txt 中的第 5 行开始(在之前的操作中删除一行后,该行实际上是空的),添加了该行,并且添加的行是 file2.txt 中的第 5 行。

命令的输出diff -u格式略有不同(所谓的“统一差异”格式)。这里向diff我们展示了一段文本,而不是两个单独的文本。该行中的@@ -1,5 +1,5 @@部分-1,5与 file1.txt 相关,部分+1,5与 file2.txt 相关。他们告诉我们,这diff将显示一段文本,从 file1.txt 中的第 1 行开始,有 5 行长。 file2.txt 也是如此 -diff显示了从第 1 行开始的 5 行。

正如我已经说过的,两个文件中的行一起显示

 this is the original text  
 line2  
-line3  
 line4  
 happy hacking !  
+GNU is not UNIX

这里-表示从 file1.txt 中删除的行,+表示添加的行。

答案2

概括:

给定 a diff file1 file2<表示该行在 中丢失file2>表示该行在 中丢失file1。和可以忽略,它们是3d2经常与 一起使用的命令。5a5patchdiff

完整答案:

许多 *nix 实用程序都提供 TeXinfo 手册以及更简单的man页面。info command例如,您可以通过运行来访问这些内容info diff。在这种情况下,您感兴趣的部分是:

2.4.2 正常格式详细说明

正常的输出格式由一组或多组差异组成;每个块都显示文件不同的一个区域。正常格式的帅哥看起来像这样:

 CHANGE-COMMAND
 < FROM-FILE-LINE
 < FROM-FILE-LINE...
 ---
 > TO-FILE-LINE
 > TO-FILE-LINE...

更改命令分为三种类型。每个文件都包含第一个文件中的行号或逗号分隔的行范围、指示要进行的更改类型的单个字符以及第二个文件中的行号或逗号分隔的行范围。所有行号都是每个文件中的原始行号。更改命令的类型有:

`LaR'
     Add the lines in range R of the second file after line L of the
     first file.  For example, `8a12,15' means append lines 12-15 of
     file 2 after line 8 of file 1; or, if changing file 2 into file 1,
     delete lines 12-15 of file 2.

`FcT'
     Replace the lines in range F of the first file with lines in range
     T of the second file.  This is like a combined add and delete, but
     more compact.  For example, `5,7c8,10' means change lines 5-7 of
     file 1 to read as lines 8-10 of file 2; or, if changing file 2 into
     file 1, change lines 8-10 of file 2 to read as lines 5-7 of file 1.

`RdL'
     Delete the lines in range R from the first file; line L is where
     they would have appeared in the second file had they not been 
     deleted.  For example, '5,7d3' means delete lines 5-7 of file 1;
     or, if changing file 2 into file 1, append lines 5-7 of file1
     after line 3 of file 2.

答案3

1)重命名文件参数以帮助您记住发生了什么,如下所示:

而不是:

diff f1 f2    # f1=file 1, and f2=file2

思考

diff file-to-edit file-with-updates

结果默认来自待编辑文件,更新自更新文件


2)此外,这些命令重命名可能会帮助您思考发生了什么:

d代表d埃莱特,但是'消除' 更清楚地表明发生了什么
A代表Add,......但是'插入' 更清楚地表明发生了什么

C代表C杭格=d+A或“删除+插入”。


像这样使用:

2,4d1 或一般 D(s)-d-N =d删除 ('消除') D 线。然后在两者的 N 行同步。

4A2,4或一般N-A-U(s) = 在第 N 行,Add('插入') 更新行 U.

注意:这两个参数几乎对称;只是从左到右颠倒了。


2,4C5,6或一般R(s)-C-U(s) = 删除 R(s) 行,然后在其位置插入更新的 U(s) 行。


例如:

4A2,4 表示从 4 开始,添加(插入)更新的第 2-4 行(即“2,4”表示第2、3、4行)

2,4d1 表示删除第 2-4 行(2、3 和 4)。

2,4C5,6 表示删除第 2-4 行(2、3 和 4),并插入更新的第 5-6 行(5 和 6)。

答案4

我建议使用:

diff -rupP file1.txt file2.txt > result.patch

然后,当您阅读时result.patch,您会立即知道其中的区别。

这些是命令行开关的含义:

-r:递归

-u:显示行号

-p(小):显示 C 函数的差异

-P(大写):如果有多个文件,则显示完整路径

相关内容