简单的 diff 命令输出

Joh*_*son 6 diff

我见过很多使用 diff 命令的例子,但没有一个详细说明它的基本用法。这是我想使用的两个文件的内容:

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

cat -A file2.txt
b$
c$
d$
e$
Run Code Online (Sandbox Code Playgroud)

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

diff file1.txt file2.txt
Run Code Online (Sandbox Code Playgroud)

我得到:

1d0
< a
4a4
> e
Run Code Online (Sandbox Code Playgroud)

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

mur*_*uru 11

diff手册

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

' f c t '
将第一个文件范围f中的行替换为第二个文件范围t中的行 。这就像添加和删除的组合,但更紧凑。例如,' 5,7c8,10' 表示将文件 1 的第 5-7 行更改为文件 2 的第 8-10 行;或者,如果将文件 2 更改为文件 1,请将文件 2 的第 8-10 行更改为文件 1 的第 5-7 行。

' r d l '从第一个文件中
删除r范围内的行;如果它们没有被删除,第l行是它们出现在第二个文件中的位置。例如,' 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!
    
    Run Code Online (Sandbox Code Playgroud)
  • 普通的:

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


b_l*_*shi 3

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

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

diff -c file1.txt file2.txt如果您想要更容易阅读的内容,那么跑步可能会更好。