sumit vedi
sumit vedi

Reputation: 777

Grepping the multiple words from file

I have one file (approx 1000 words).

1.txt:

XYZ

ABC

DEF

GHI

...

And I have another file 2.txt which contains some data, now I want to grep these words in 1.txt in the file 2.txt. I have used the below logic, but an error is given.

name=$(cat 1.txt |tr '\n' '|')

grep -E -w ${name} 2.txt

Upvotes: 0

Views: 389

Answers (2)

kdubs
kdubs

Reputation: 1722

you need quotes and use egrep

egrep -w "$(cat 1.txt |tr '\n'  '|')" 2.txt

but the answer with -f is better

Upvotes: 0

Chris Seymour
Chris Seymour

Reputation: 85795

Grep can read patterns from a file using the -f option:

egrep -w -f 1.txt 2.txt 

Upvotes: 3

Related Questions