Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get only added/changed lines in diff?

Tags:

shell

diff

a.txt

1
2
3
4
5
6

b.txt

10
2
3
40
50
6
70

I'd like to run some command on these files that generates the following output.

10
40
50
70

How can I run a diff on two files but only show lines that changed. I don't want any other metadata around the output.

I also don't want to see any context around the changed lines.

like image 331
Mulan Avatar asked Feb 12 '23 18:02

Mulan


2 Answers

Try

comm -1 -3 a.txt b.txt

comm, common lines, is a handy command.

like image 190
Brian Tiffin Avatar answered Feb 19 '23 07:02

Brian Tiffin


Actually I like Brian's answer using "comm" a lot. It was new to me and works for me.

My more complicated method would be to use a chain of diff, grep and then sed to remove the first two characters.

diff a.txt  b.txt  | grep ">" | sed  s/..//

Not beautiful, not bullet-proof, but a quick hack.

like image 20
Mathias Poths Avatar answered Feb 19 '23 06:02

Mathias Poths