user3242440
user3242440

Reputation: 111

Comparing two files with awk and output non matching lines

I have two files with uneven columns and I want to compare these two files and remove the matching line from file1

file 1:

nt1  ID420
nt1  ID42
nt56 ID6008
ht44 ID555
lt12 ID34
lt12 ID3434
ntt56 ID667
klll ID87693

file2

nt23 ID42
ht44 ID555
lt12 ID3434

Desired ouput:

nt1 ID420
nt56 ID6008
lt12 ID34
ntt56 ID667
klll ID87693

I have used below command from earlier post but result is not exactly matching and that is taking initial character for matching. I do not have much exp on awk commands and I need awk single line command or sed or similar shell script to get this output. Thank you well in advance:

awk -FS=" " 'NR==FNR {b[$0]; next} {for (x in b) if($0 ~ x) next;print $0}' file2 file1 > outputfile

Upvotes: 4

Views: 17460

Answers (1)

glenn jackman
glenn jackman

Reputation: 247162

This will be the fastest:

grep -vFxf file2 file1

With awk:

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

If you don't care that the output is sorted, this is what comm is for:

comm -23 <(sort file1) <(sort file2)

Upvotes: 11

Related Questions