1

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.

SajithP
  • 592
  • 8
  • 19
Azarea
  • 516
  • 7
  • 22
  • 1
    Possible duplicate of [Fast way of finding lines in one file that are not in another?](https://stackoverflow.com/questions/18204904/fast-way-of-finding-lines-in-one-file-that-are-not-in-another) – Benjamin W. Aug 11 '17 at 05:48
  • See also [BashFAQ/036](http://mywiki.wooledge.org/BashFAQ/036): "How can I get all lines that are: in both of two files (set intersection) or in only one of two files (set subtraction)." – Benjamin W. Aug 11 '17 at 05:49
  • you are able to obtain the needed result with "native" diff – RomanPerekhrest Aug 11 '17 at 06:08

3 Answers3

3

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.

dimo414
  • 47,227
  • 18
  • 148
  • 244
1

Native diff solution:

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

The output:

d
e
f
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

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
James Brown
  • 36,089
  • 7
  • 43
  • 59