Mulan
Mulan

Reputation: 135415

How to get only added/changed lines in 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.

Upvotes: 8

Views: 8346

Answers (3)

Mathias Poths
Mathias Poths

Reputation: 61

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.

Upvotes: 5

Mulan
Mulan

Reputation: 135415

@Brian Tiffin's answer may work for some people.

If you're having trouble with it, I was able to get this working

$ diff -U0 a.txt b.txt | grep ^+ | sed -e /s^+//

Upvotes: 1

Brian Tiffin
Brian Tiffin

Reputation: 4126

Try

comm -1 -3 a.txt b.txt

comm, common lines, is a handy command.

Upvotes: 9

Related Questions