VDC
VDC

Reputation: 1449

Find the difference between two files

I have the following situation:

The file1.dat is like:

1 2
1 3
1 4
2 1

and the file2.dat is like:

1 2
2 1
2 3
3 4

I want to find the differences between the second file from the first. I tried wit grep -v -f file1 file2 but my real files are bigger than this two and when I tried with it the shell never ended is work.

The result should be:

2 3
3 4

The files are sorted and they have the same number of elements. Any way to find a solution with awk?

Upvotes: 1

Views: 9833

Answers (1)

Chris Seymour
Chris Seymour

Reputation: 85883

Seems like you want lines in file2 that are not in file1:

$ awk 'FNR==NR{a[$0];next}!($0 in a)' file1 file2
2 3
3 4

However it's simpler to use comm:

$ comm -13 file1 file2
2 3
3 4

Upvotes: 13

Related Questions