Compare 2 files and find missing fields awk

Hello experts!
I have 2 files.

file1 is a list file containing uniquely names. e.g.:

name1 number number
name2 number number
name5 number number
name10 number number
...

file2 is a data file arbitrary containing the names of file1 in paragraphs separated by "10" e.g.

name4   random_number
name5 random_nuber
10
name4   random_number
name6   random_number
name10 random_number
10
...

what I want is to get the names of file2 that is missing from file1 e.g

name4
name6

I have tried with

 awk ' FNR==NR{a[$1]++;next}!a[$1] { print $1}'  file1 file2

but I get all the 10s of file2 and repeatingly the sequence of stations
e.g.

10
10
10
name6
10
name6
10
...

Thank you in advance!

awk 'NR==FNR{A[$1];next}!($1 in A)&&$1!=10{M[$1]}END{for(k in M) print k}' file1 file2
1 Like

Hello phaethon,

Following may help you in same, considering that 1st field will always have string name in it. Please do let us know if you have more conditions too to get your output.

awk 'FNR==NR{if($1 ~ /name/){A[$1]=$0;next}} ($1 in A){delete A[$1]} END{for(i in A){print i}}' file2 file1
 

Output will be as follows.

name4
name6
 

Thanks,
R. Singh

1 Like

thank you it worked!