如何理解linux中的diff -u?

Nic*_*Wei 4 linux bash shell terminal

示例代码

diff -r -u -P a.c b.c > diff.patch
Run Code Online (Sandbox Code Playgroud)

我试图在人中搜索。

man 说 diff -u 是为了统一输出的模式,什么意思,什么时候用?

多谢。

hig*_*aro 7

来自维基百科(差异实用程序)

统一格式(或unidiff)继承上下文格式做出的技术改进,但产生较小的差异与新老文字呈现紧邻。通常使用“-u”命令行选项调用统一格式。此输出通常用作补丁程序的输入。许多项目特别要求以统一格式提交“差异”,使得统一差异格式成为软件开发人员之间最常用的交换格式。

...

该格式以与上下文格式相同的两行标题开头,不同之处在于原始文件"---""+++". 接下来是一个或多个包含文件中行差异的更改块。未更改的上下文行以空格字符开头,添加行以加号开头,删除行以减号开头。

一个大块以范围信息开始,紧随其后的是行添加、行删除和任意数量的上下文行。范围信息由双符号包围,并将出现在上下文格式(上图)中的两行中的内容组合到一行中。范围信息行的格式如下:

    @@ -l,s +l,s @@ optional section heading
Run Code Online (Sandbox Code Playgroud)

...

任何格式的想法都是按照一系列步骤diff文件转换为目标文件。让我们看一个简单的例子,说明这是如何使用统一格式的

鉴于以下文件:

来自.txt

a
b
Run Code Online (Sandbox Code Playgroud)

to.txt

a
c
Run Code Online (Sandbox Code Playgroud)

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

    @@ -l,s +l,s @@ optional section heading
Run Code Online (Sandbox Code Playgroud)

解释。标头说明:

a
b
Run Code Online (Sandbox Code Playgroud)

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

a
c
Run Code Online (Sandbox Code Playgroud)

接下来,更改列表:

--- 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
Run Code Online (Sandbox Code Playgroud)

以下是一些 StackOverflow 答案,其中包含有关此主题的非常好的信息:

/sf/answers/766534751/
/sf/answers/177100871/

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

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


Tho*_*key 3

统一这个词是被创造出来的。更好的做法或许是称之为“简洁”。

要点diff -u是它是比上下文差异更简洁的表示。引用 Wayne Davison在comp.sources.miscunidiff上发布的原始描述(第 14 卷,90 年 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.  [...]
Run Code Online (Sandbox Code Playgroud)

以下是一些有用的链接:

没有用(并且具有误导性)