本页目录
- 安装 diff3
- diff3 的用法
在我们早先的一篇教程中,我们讨论了一个名为 diff 的 Linux 命令行工具,它可以让你逐行比较两个文件。但如果需求是比较三个文件而非两个,该怎么办?是的,Linux 下也有这样的命令行工具,它被恰当地命名为 diff3。
如果你一直在寻找这样的工具,那么不用再找了。在本教程中,我们将通过易于理解的示例来解释 diff3 的用法。
安装 diff3
diff3 工具很可能已经默认安装在你的系统上——至少在 Ubuntu 上是这样。但如果没有,也不必担心,你可以通过安装 GNU Diffutils 软件包来轻松完成。有关如何在你的系统上下载并安装该软件包的说明,请参考相应的文档。
diff3 的用法
由于 diff3 工具比较三个文件,因此在我们的用法示例中,我们需要准备三个文件,将它们作为输入传递给该工具。
这是 file1:
This is line1 with some change This is line2 This is line3
这是 file2:
This is line1 This is line2 This is line3
这是 file3:
This is line1 This is line2 This is line3
显然,如果我们查看这三个文件的内容,唯一的变化出现在第一个文件的第一行。所以现在让我们看看 diff3 是如何报告这一变化的。
以下是我们在此情况下使用 diff3 的方法:
diff3 file1 file2 file3
以下是它产生的输出:
====1 1:1c This is line1 with some change 2:1c 3:1c This is line1
你可以通过以下方式理解该输出:
- 以四个 '=' 号开头、以数字 '1' 结尾的第一行表示变化发生在第一个文件中。
- 在下一行中,第一个 '1' 表示 file1;冒号后的 '1c' 表示“第一行包含”。实际的行显示在输出的下一行。
- 类似地,2:1c 和 3:1c 表示“file2 和 file3 的第一行包含”。由于该行在两个文件中的内容相同,实际行只在输出末尾显示一次。
为了更清楚,我们再举一个变化发生在 file3 中的例子。
这是 file1:
This is line1 This is line2 This is line3
这是 file2:
This is line1 This is line2 This is line3
这是 file3:
This is line1 with some change This is line2 This is line3
命令保持不变:
diff3 file1 file2 file3
以下是输出:
====3 1:1c 2:1c This is line1 3:1c This is line1 with some change
因此,在这种情况下,该输出可以理解为:
- 变化发生在 file3 中
- file1 和 file2 都以“This is line1”作为它们的第一行
- 而 file3 则以“This is line1 with some changes”作为它的第一行。
以上介绍了 diff3 如何报告行中的变化。现在让我们看看,如果其中一个文件包含额外的一行,diff3 会如何表现:
虽然 file1 和 file2 保持不变,以下是 file3:
This is line1 This is line2 This is line3 This is line4
从其内容可以清楚地看出,file3 现在在末尾包含了一个额外的行。
让我们运行相同的 diff3 命令:
diff3 file1 file2 file3
以下是该命令产生的输出:
====3 1:3a 2:3a 3:4c This is line4
输出开头说明变化发生在 file3 中。该变化位于 file3 的第四行(3:4c),与 file1 和 file2 相比,这是全新的一行。
我们到目前为止讨论的都是一些简单直观的情况。让我们来看几个稍微复杂一点的情况。例如,假设 file1、file2 和 file3 包含以下内容:
This is line1 This is line2 with some change This is line3
This is line1 This is line2 This is line3
This is line1 This is line2 This is line3 with some change
所以现在 file1 和 file2 都包含变化,而且是在不同的行中。我们来看看 diff3 在这种情况下会产生什么:
==== 1:2,3c This is line2 with some change This is line3 2:2,3c This is line2 This is line3 3:2,3c This is line2 This is line3 with some change
正如你在上面显示的输出中所看到的,第一行不包含任何数字。那是因为现在不是只有一个文件,而是多个文件都包含了变化。对于 diff3 来说,三个文件中的第 2 行和第 3 行现在都不同了。这就是为什么在冒号(:)之后不再是单个数字,而是输出现在包含了一个范围——'2,3',表示第 2 行到第 3 行。
让我们再看一个例子。假设这次三个文件包含以下信息:
This is line1 This is line2 This is line3 This is line4
This is line1 This is line2 This is line3
This is line1 This is line2 This is line3 This is line4 This is line5
现在当运行 diff 命令时,它会产生以下输出:
==== 1:4c This is line4 2:3a 3:4,5c This is line4 This is line5
在这里,如果你试着理解,diff3 命令表示变化发生在所有三个文件的第 3 行之后。file1 以其第四行包含“This is line4”,file3 以其第四和第五行分别包含“This is line4”和“This is line5”。而 file2 只有 3 行。
以上就是关于 diff3 基本用法的全部内容。值得一提的是,以下是官方文档对该工具的说明:“你可以使用 diff3 命令来显示三个文件之间的差异。当两个人各自对同一个原始文件进行了独立修改时,diff3 可以报告原始文件与两个修改后的版本之间的差异,并可以生成一个合并文件,其中同时包含两个人的修改,并对冲突发出警告。”
结论
我们这里只是触及了皮毛,diff3 还能做更多的事情。例如,正如官方文档所指出的,你也可以用它来合并修改。此外还有许多命令行选项,可以让你自定义命令的输出,并访问它提供的其他一些功能。这些内容都在 man page 上,请务必仔细阅读。你可能还想查看我们的 sdiff 命令教程