How to grep exact matches from a string list file

I have file A with one column with a list of such rows:

ADAMTS9
AIP
....

I want to use lines in file A for grep lines containing them in file B, and file B looks like this:

chr13   50571142        50592603        ADAMTS9  21461   +
chr19   50180408        50191707        AIP   11299   +
chr19   50180408        50193000        AIP-S1   6532    -

I used:

grep -F -i -w -f A B 

and he was able to grep all 3 lines above. However, I want the first two lines to be grep'ed, and the third line with AIP-S1 is not an exact match with AIP ..

Can someone tell me how to fix this command?

Thanks.

+4
source share
2 answers

Instead, you can use awk:

awk 'FNR==NR{a[$1];next} ($4 in a)' A B
chr13   50571142        50592603        ADAMTS9  21461   +
chr19   50180408        50191707        AIP   11299   +

OR to search in any field:

awk 'FNR==NR{a[$1];next} {for (i=1; i<=NF; i++) if ($i in a) print}' A B
+3
source

-w . , "AIP-S1" "-" . , "AIP" .

, , ", ":

$ grep -if <(sed 's/^/\\(^\\|[[:space:]]\\)/; s/$/\\($\\|[[:space:]]\\)/' A) B
chr13   50571142        50592603        ADAMTS9  21461   +
chr19   50180408        50191707        AIP   11299   +
+2

Source: https://habr.com/ru/post/1529254/


All Articles