charles
charles

Reputation: 55

find list of patterns from file not matching in another file unix

I can't seem to be able to wrap my head around this simple problem. I need to print all patterns that are in fileA but not int fileB.

Consider fileA to be as :

aaa
bbb
ccc
ddd

And consider fileB to be as :

ppppppppppppp_aaa_ppppppppppppp
ppppppppppppp_ccc_ppppppppppppp
ppppppppppppp_ddd_ppppppppppppp

I want the following result :

bbb

I have tried

grep -f -v fileA fileB
grep -F -v -f fileA fileB

but it does not seems to work as nothing is printed.

How can I achieve this in a unix command?

Thank you

Upvotes: 2

Views: 1209

Answers (2)

Vijay
Vijay

Reputation: 67221

awk -F'_' 'FNR==NR{a[$2]=$1;next}!(a[$0]){print}' fileB fileA

Tested below:

> cat fileA
ppppppppppppp_aaa_ppppppppppppp
ppppppppppppp_ccc_ppppppppppppp
ppppppppppppp_ddd_ppppppppppppp
> cat fileB
aaa
bbb
ccc
ddd
> awk -F'_' 'FNR==NR{a[$2]=$1;next}!(a[$0]){print}' fileA fileB
bbb
> 

Upvotes: 1

EJK
EJK

Reputation: 12527

Try this:

#!/bin/bash
exec < fileA
while read line; do
    grep -q "$line" fileB || echo "$line"
done

Upvotes: 2

Related Questions