简单的 diff 命令输出

简单的 diff 命令输出

我见过很多使用 diff 命令的示例,但没有一个详细介绍其基本用法。以下是我想要使用的两个文件的内容:

cat -A file1.txt
a$
b$
c$
d$

cat -A file2.txt
b$
c$
d$
e$

如果我像这样使用 diff 命令:

diff file1.txt file2.txt

我得到:

1d0
< a
4a4
> e

我想知道的是,除了小于号和大于号之外,1d0 中的第 1 行和第 0 行以及 4a4 中的第 4 行和第 4 行是什么意思。更一般地说,为什么 a 前面有一个小于号而不是大于号?有什么区别?

答案1

手册diff

Ar'
添加范围内的行r第二个文件的行后第一个文件的。例如,' 8a12,15' 表示将文件 2 的第 12-15 行附加到文件 1 的第 8 行之后;或者,如果将文件 2 更改为文件 1,则删除文件 2 的第 12-15 行。

FC'
替换范围内的行F第一个文件包含范围内的行 第二个文件的。这类似于添加和删除的组合,但更紧凑。例如,' 5,7c8,10' 表示将文件 1 的第 5-7 行更改为文件 2 的第 8-10 行;或者,如果将文件 2 更改为文件 1,则将文件 2 的第 8-10 行更改为文件 1 的第 5-7 行。

rd'
删除范围内的行r从第一个文件;行是它们在第二个文件中未删除时出现的位置。例如,' 5,7d3' 表示删除文件 1 的第 5-7 行;或者,如果将文件 2 更改为文件 1,则将文件 1 的第 5-7 行附加到文件 2 的第 3 行之后。

如果你看>一下<并排输出格式

<'
两个文件有所不同,只有第一个文件包含该行。

>'
两个文件有所不同,只有第二个文件包含该行。

手册的输出示例:

  • 并排:

    The Way that can be told of is n   <
    The name that can be named is no   <
    The Nameless is the origin of He        The Nameless is the origin of He
    The Named is the mother of all t   |    The named is the mother of all t
                                       >
    Therefore let there always be no        Therefore let there always be no
      so we may see their subtlety,           so we may see their subtlety,
    And let there always be being,          And let there always be being,
      so we may see their outcome.            so we may see their outcome.
    The two are the same,                   The two are the same,
    But after they are produced,            But after they are produced,
      they have different names.              they have different names.
                                       >    They both may be called deep and
                                       >    Deeper and more profound,
                                       >    The door of all subtleties!
    
  • 普通的:

    1,2d0
    < The Way that can be told of is not the eternal Way;
    < The name that can be named is not the eternal name.
    4c2,3
    < The Named is the mother of all things.
    ---
    > The named is the mother of all things.
    > 
    11a11,13
    > They both may be called deep and profound.
    > Deeper and more profound,
    > The door of all subtleties!
    

答案2

diff命令详细解释这里。您会在页面顶部附近的“diff 工作原理”标题下找到您要查找的内容。

具体来说,1d0这意味着您必须从第一个文件中删除一行,以便它们同步到第 0 行。这不是文件的第一行,它基本上是说。如果您进行了该删除,则两个文件都将从空点开始。文件 1 中的下一行输出是两个文件中的第一行输出(即下一行应该是第 1 行)。

diff -c file1.txt file2.txt如果您想要一些更容易阅读的东西,那么您最好跑步。

相关内容