如何理解linux中的diff -u?

时间:2015-03-17 08:34:37

标签: linux bash shell terminal

示例代码

diff -r -u -P a.c b.c > diff.patch

我试图在男人身上搜寻。

man说diff -u是统一输出模式,它的含义是什么,我们什么时候应该使用它?

非常感谢。

2 个答案:

答案 0 :(得分:3)

统一统一一词。更好,或许本来应该称之为"简洁"。

diff -u的要点是它比上下文差异更简洁。引自Wayne Davison的原始描述,unidiff发布到 comp.sources.misc (第14卷,1988年8月31日):

I've created a new context diff format that combines the old and new chunks into 
one unified hunk.  The result?  The unified context diff, or "unidiff."         

Posting your patch using a unidiff will usually cut its size down by around     
25% (I've seen from 12% to 48%, depending on how many redundant context lines   
are removed).  Even if the diffs are generated with only 2 lines of context,    
the savings still average around 20%.                                           

Keep in mind that *no information is lost* by the conversion process.  Only
the redundancy of having multiple identical context lines.  [...]

以下是一些有用的链接:

没用(并且误导)

答案 1 :(得分:2)

来自Wikipedia (diff utility)

  

统一格式(或 unidiff )继承了上下文格式所做的技术改进,但产生了一个较小的差异,其中新旧文本紧邻相邻。统一格式通常使用" -u"命令行选项。此输出通常用作补丁程序的输入。许多项目特别要求"差异"以统一格式提交,使统一的diff格式成为软件开发人员之间交流的最常见格式。

     

...

     

格式以与上下文格式相同的两行标题开头,但原始文件前面有"---",新文件前面有"+++"。在此之后是一个或多个包含文件中行差异的更改。未更改的上下文行前面有空格字符,附加行前面带有加号,删除行前面带有减号。

     

hunk以范围信息开头,紧接着是行添加,行删除和任意数量的上下文行。范围信息由双重符号包围,并组合成一行,以上下文格式出现在两行上(上图)。范围信息行的格式如下:

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

...

diff抛出的任何格式的想法是按照一系列步骤将文件转换为目标文件。让我们看一个简单的示例,了解它如何与统一格式一起使用。

给出以下文件:

from.txt

a
b

to.txt

a
c

diff -u from.txt to.txt的输出是:

--- frokm.txt   2015-03-17 04:34:47.076997087 -0430
+++ to.txt      2015-03-17 04:35:27.872996388 -0430
@@ -1,2 +1,2 @@
 a
-b
+c

解释。标题说明:

--- from.txt    2015-03-17 22:42:18.575039925 -0430  <-- from-file time stamp
+++ to.txt      2015-03-17 22:42:10.495040064 -0430  <-- to-file time stamp

这个差异只包含一个大块(只有一组更改将文件 form.txt 转换为 to.txt ):

@@ -1,2 +1,2 @@  <-- A hunk, a block describing chages between both files, there could be several of these in the diff -u output
   ^    ^
   |   (+) means that this change starts at line 1 and involves 2 lines in the to.txt file
  (-) means that this change starts at line 1 and involves 2 lines of the from.txt file

接下来,更改列表:

 a   <-- This line remains the same in both files, so it won't be changed
-b   <-- This line has to be removed from the "from.txt" file to transform it into the "to.txt" file
+c   <-- This line has to be added to the "from.txt" file to transform it into the "to.txt" file

以下是一些StackOverflow答案以及关于此主题的非常好的信息:

https://stackoverflow.com/a/10950496/1041822
https://stackoverflow.com/a/2530012/1041822

以及其他一些有用的文档:

https://linuxacademy.com/blog/linux/introduction-using-diff-and-patch/ http://www.artima.com/weblogs/viewpost.jsp?thread=164293