Linux diff只输出输出中的行号

时间:2016-07-06 16:30:42

标签: linux diff

我想使用linux diff命令获取以下输出:

2,4c2,4

我只想知道文件不同的行号。我不想要控制台上的实际线路。

例如:

如果我将执行以下命令: diff file1.txt file2.txt

我想要以下输出:

2,4c2,4

我不想要输出:

2,4c2,4
< I need to run the laundry.
< I need to wash the dog.
< I need to get the car detailed.
---
> I need to do the laundry.
> I need to wash the car.
> I need to get the dog detailed.

我查看了diff命令的手册,但我找不到任何可以让我达到我想要的选项。

2 个答案:

答案 0 :(得分:1)

将其传输到grep并仅显示以数字开头的行。

diff file1.txt file2.txt | grep '^[1-9]'

答案 1 :(得分:0)

传递标志-f。

-sh-4.1$ cat file1.txt 
 I need to run the laundry.
 I need to wash the dog.
 difdferen line 
 I need to get the car detailed.


-sh-4.1$ cat file2.txt 
 I need to run the laundry.
 I need to wash the dog.
 I need to get the car detailed.

-sh-4.1$ diff -f file1.txt file2.txt 
d3

根据@Barmar评论编辑:因为它可以处理更改的行...您可以通过询问以"<" or ">"

开头的行的倒数来过滤"<" or ">"

首先:普通差异:

-sh-4.1$ diff file*
3d2
<  difdferen line 
4a4
>  different line in file2
-sh-4.1$ 

使用grep过滤以&lt;开头的行或者&gt;

  -sh-4.1$ diff   file* | egrep -v "^<|^> |^-"

3,4d2 5A4

3d2
4a4

@Barmar建议的简化版

-sh-4.1$ diff file1.txt file2.txt  | egrep -v  "^[-<>]"
3,4d2
5a4