Chris Dias
Chris Dias

Reputation: 87

How to find lines containing any string from another file?

I have 2 csv files. File A, with multiple columns. File B, with one column. eg.:

File A:

chr1 100000 100022 A C GeneX
chr2 200000 200033 X GeneY
chr3 300000 300055 G A GeneZ

File B:

GeneY
GeneZ

I would want my output to be:

chr2 200000 200033 X GeneY
chr3 300000 300055 G A GeneZ

I have tried using grep (which crashes) and others.
I am certain there must be a very simple answer to this that I just can't see!

Upvotes: 7

Views: 4574

Answers (2)

Jotne
Jotne

Reputation: 41460

Here is how to do it with awk

awk 'FNR==NR {a[$0];next} {for (i in a) if (i~$1) print i}' FileA FileB
chr2 200000 200033 X GeneY
chr3 300000 300055 G A GeneZ

Or like this:

awk 'FNR==NR {a[$0];next} ($NF in a)' FileB FileA
chr2 200000 200033 X GeneY
chr3 300000 300055 G A GeneZ

Upvotes: 0

Amit
Amit

Reputation: 20516

Use grep -f

grep -f FileB FileA

Upvotes: 3

Related Questions