简单的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  e 

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

来自diff手册 :

是一个
在第一个文件的第l行之后添加第二个文件的范围r中的行。 例如,’ 8a12,15 ‘表示在文件1的第8行之后追加文件2的第12-15行; 或者,如果将文件2更改为文件1,则删除文件2的第12-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中的行; 如果没有删除它们,它们就会出现在第二个文件中。 例如,’ 5,7d3 ‘表示删除文件1的第5-7行; 或者,如果将文件2更改为文件1,则在文件2的第3行之后追加文件1的第5-7行。

如果你看一下并排的输出格式 , ><有意义:

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

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

手册的示例输出:

  • 并排侧:

     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! 

diff命令在这里详细解释。 你会在页面顶部“How diff Works”标题下找到你要找的东西。

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

如果你想要一些更容易阅读的东西,你可能最好运行diff -c file1.txt file2.txt