Azarea
Azarea

Reputation: 536

linux shell diff two files to get new line

I have two files and I want to get the new line by comparing two files, I know can use 'diff newfile oldfile' to get the new lines, but the output will include "<" and diff infomation which I don't want to have.

for example, now I have an oldfile:

a
b
c

and a newfile

a
b
c
d
e
f

the result of the 'diff newfile oldfile' will be

4,6d3
< d
< e
< f

but the result i want to have is

d
e
f

So how can i get this output? I have searchd many diff options but dont have any ideas

Thank you in advance.

Upvotes: 1

Views: 2252

Answers (3)

RomanPerekhrest
RomanPerekhrest

Reputation: 92894

Native diff solution:

diff --changed-group-format='%<' --unchanged-group-format='' new.txt old.txt

The output:

d
e
f

Upvotes: 1

James Brown
James Brown

Reputation: 37464

You could also use awk:

$ awk 'NR==FNR{a[$0];next} ($0 in a==0)' oldfile newfile
d
e
f

or grep if the files are not that big (mind the partial matches):

$ grep -v -f oldfile newfile
d
e
f

or join (inputfiles need to be ordered):

$ join -v 2 oldfile newfile
d
e
f

Upvotes: 0

dimo414
dimo414

Reputation: 48874

Similar to this question, you can use comm for this purpose.

comm -13 file1 file2

Will print only the lines of file2 that don't exist in file1.

Upvotes: 3

Related Questions