Aaron Perry
Aaron Perry

Reputation: 1051

Use Defined Variable with egrep to Match Contents Inside Text File

I have a text file with numerous four-letter codes. I need to use the contents of this text file to match the content with the text inside of another text file and then grep out the contents of the matched lines to a new text file.

I can't get the below code to work after much avail...

while read site

do

egrep -n "${site}" cy.00.txt > file.txt

done < /home/weather/newsites.txt

newsites.txt:

KBED
KBOS
KDCA

Expected Output:

832:KBED   XXX    7/10/2015  0000 UTC                      
1060:KBOS   XXX    7/10/2015  0000 UTC                      
1630:KDCA   XXX    7/10/2015  0000 UTC    

Upvotes: 2

Views: 85

Answers (1)

Jahid
Jahid

Reputation: 22428

This should do wihout any loop:

egrep -nf newsites.txt cy.00.txt >file.txt

And also consider using grep -E instead of egrep. man grep says:

egrep is the same as grep -E. ... Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified.

Upvotes: 4

Related Questions