Compare two files with awk or sed Compare two files with awk or sed shell shell

Compare two files with awk or sed


 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 :

15679


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.


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'1567910

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.