Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two files with awk and output non matching lines

Tags:

awk

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
like image 786
user3242440 Avatar asked Oct 19 '25 14:10

user3242440


1 Answers

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)
like image 97
glenn jackman Avatar answered Oct 21 '25 11:10

glenn jackman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!