Programmer
Programmer

Reputation: 8697

awk search a line withing a file

I am trying to find occurrences of a STRING in some other file:

First I extract the STRING exactly I want to search:

grep STRING test.txt | cut -d"," -f3 | tr -d ' ' 

Now I proceed to search it in other file - so my command is:

grep STRING test.txt | cut -d"," -f3 | tr -d ' ' | awk '/$0/' temp.txt

I am getting 0 rows output - but comparing manually I do find the strings common in both files?

Upvotes: 0

Views: 117

Answers (1)

Steve
Steve

Reputation: 54392

You can't pipe like that. You'd need to use a sub-shell; something like:

grep $(grep STRING test.txt | cut -d"," -f3 | tr -d ' ') temp.txt

Alternatively, use awk like this:

awk -F, 'FNR==NR && /STRING/ { gsub(/ /,""); a[$3]; next } FNR!=NR { for (i in a) if ($0 ~ i) { print; next } }' test.txt temp.txt

Upvotes: 1

Related Questions