linux - 如何理解 Linux 中的 diff -u?

标签 linux bash shell terminal

示例代码

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

我试着在 man 中搜索。

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

非常感谢。

最佳答案

来自 Wikipedia (diff utility) :

The unified format (or unidiff) inherits the technical improvements made by the context format, but produces a smaller diff with old and new text presented immediately adjacent. Unified format is usually invoked using the "-u" command line option. This output is often used as input to the patch program. Many projects specifically request that "diffs" be submitted in the unified format, making unified diff format the most common format for exchange between software developers.

...

The format starts with the same two-line header as the context format, except that the original file is preceded by "---" and the new file is preceded by "+++". Following this are one or more change hunks that contain the line differences in the file. The unchanged, contextual lines are preceded by a space character, addition lines are preceded by a plus sign, and deletion lines are preceded by a minus sign.

A hunk begins with range information and is immediately followed with the line additions, line deletions, and any number of the contextual lines. The range information is surrounded by double-at signs, and combines onto a single line what appears on two lines in the context format (above). The format of the range information line is as follows:

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

...

diff 向您提出的任何格式的想法都是通过一系列步骤将 文件转换为目标 文件。让我们看一个简单的例子,说明它如何使用统一格式

给定以下文件:

来自.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

解释。 header 说明:

--- 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

这个 diff 只包含一个 block (只有一组更改将文件 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

关于linux - 如何理解 Linux 中的 diff -u?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29094454/

相关文章:

linux - 进程组必须有一个正在运行的领导进程吗?

Linux x86 汇编打印问题

linux - 我们如何使用 sed 命令在文件中写入\n?

xml - 在 shell 脚本中使用 sed 命令在 XML 文件中添加 XML 元素

bash - 运行具有多个并行动态程序的 Shell 脚本

python - 为什么要在 Fabric 脚本中逐行重新执行 shell 命令?

linux - rasm 和 ndisasm

Linux内核编程: can't include header file

linux - 如何在 Linux 中打开一个程序的多个实例

linux - 使用 TCL 驱动 linux shell