我想使用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
命令的手册,但我找不到任何可以让我达到我想要的选项。
答案 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