AWE
AWE

Reputation: 4135

Compare two files with awk or sed

This must be easy for you

Here is file1 (one column)

1
2
3
4
5
6
7
8
9

and here is file2 (two columns)

2 yay
3 ups
4 wow
8 hey

There must be a simple one liner to print out lines in file1 that don´t match to file2

Upvotes: 3

Views: 7680

Answers (3)

potong
potong

Reputation: 58568

This sed solution might work for you:

{ seq 1 10; echo -e "2 yay\n3 ups\n4 wow\n8 hey"; } | sort -n | 
sed '1{h;d};H;${x;s/\(\S\+\)\n\1[^\n]*\n//g;p};d'
1
5
6
7
9
10

Explanation: Sort the files numerically then using sed slurp the file in to the hold space (HS). At end of file swap to the HS and then delete lines with the duplicate keys.

Upvotes: 2

Kent
Kent

Reputation: 195239

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

or using join with -v, as @Michael suggested:

join -v1 file1 file2

both will print :

1
5
6
7
9

Upvotes: 15

Michael J. Barber
Michael J. Barber

Reputation: 25052

You can do this by combining cut and comm:

cut -d' ' -f1 file2 | comm -13 - file1

You might also consider join, depending on how you want to handle repeated lines.

Upvotes: 3

Related Questions