Grep a pattern given in one file at other file and display its corresponding contents as output.

*****************************************
Right now i have this current system.

I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg.

"this is the line1"
"this is the line2"

The yyy.txt with lot of lines. eg:

"This is a test message which contains rubbish information just to fill the page which is of no use. this is the line1 which is suppose to be grepped and displayed as a output."

My output is to search the pattern given in xxx.txt at yyy.txt and display the corresponding line as output.

Eg output: this is the line1 which is suppose to be grepped and displayed as a output.

The following script does this.(thanks rdcwayx for giving this solution)

awk -F \" 'NR==FNR {a[$2];next} {for (i in a) if ($0 ~ i) print }'  xxx.txt yyy.txt

*****************************************

My proposed system is as follows.

eg:

I have two files say aaa.txt and bbb.txt.

aaa.txt with list of patterns in double quotes and its corresponding output which has to be displayed.

"this is the line1"=line one found
"this is the line2"=line two found

bbb.txt with lot of lines. eg:

"This is a test message which contains rubbish information just to fill the page which is of no use.
 this is the line1 which is suppose to be grepped and displayed as a output."

my requirement is to search the pattern given in double quotes in aaa.txt at bbb.txt and if it exists in bbb.txt, the corresponding pattern after = sign in aaa.txt should be displayed.

Eg output: line one found

Please let me know if i am not clear.

Hi,

Modified your existing code,

awk -F"[\"=]" 'NR==FNR{a[$2]=$4;next} {for (i in a){if($0 ~ i){print a}}}' aa.txt bb.txt
1 Like

Awesome thanks :slight_smile:

Is it possible to get the line number of the occurrence along with the output? from the above example, "this is the line1 which is suppose to be grepped and displayed as a output." is at second line. So the output should be as,

2: line one found

Tr this,

awk -F"[\"=]" 'NR==FNR{a[$2]=$4;next} {for (i in a){if($0 ~ i){print FNR" : "a}}}' aa.txt bb.txt
1 Like

Thanks for your contribution.
I am facing one more challenge. Please help me.

My proposed system is as follows.

eg:

I have two files say aaa.txt and bbb.txt.

aaa.txt with list of patterns in double quotes and its corresponding output which has to be displayed.

"this is the line1"=line one found
"this is the line2"=line two found

bbb.txt with lot of lines. eg:

"This is a test message which contains rubbish information just to fill the page which is of no use.
 this is the line1 which is suppose to be grepped and displayed as a output.
this is the line2 with specific pattern AABCDEFGHI which is to be grepped"

Requirement 1 : my requirement is to search the pattern given in double quotes in aaa.txt at bbb.txt and if it exists in bbb.txt, the corresponding pattern after = sign in aaa.txt should be displayed.

Requirement 2 : If there is any line which has a 10 digit pattern starts with AA, the entire pattern has to be displayed. If there is no such pattern, this requirement can be ignored.

Eg output:

2: line one found
3: AABCDEFGHI

Right now i am provided withe code to get Requirement 1 with line number. Please help me to get requirement 2.

 awk -F"[\"=]" 'NR==FNR{a[$2]=$4;next} {for (i in a){if($0 ~ i){print FNR" : "a}}}' aa.txt bb.txt

Try this,

 awk -F"[\"=]" 'NR==FNR{a[$2]=$4;next} {for(i=1;i<=NF;i++) {if($i ~ /^AA/ && length($i)==10) {print FNR" : "$i;next}}} {for (j in a){if($0 ~ j){print FNR" : "a[j]}}}' aa.txt FS=" " bb.txt

It simply ignores the code. Not even displaying an empty line. i am not sure if i am missing something. Pls help :frowning:

Hi,
It's Working fine at my end.

# cat aa.txt
"this is the line1"=line one found
"this is the line2"=line two found
# cat bb.txt
"This is a test message which contains rubbish information just to fill the page which is of no use.
 this is the line1 which is suppose to be grepped and displayed as a output.
this is the line2 with specific pattern AABCDEFGHI which is to be grepped"
# awk -F"[\"=]" 'NR==FNR{a[$2]=$4;next} {for(i=1;i<=NF;i++) {if($i ~ /^AA/ && length($i)==10) {print FNR" : "$i;next}}} {for (j in a){if($0 ~ j){print FNR" : "a[j]}}}' aa.txt FS=" " bb.txt

O/P

2 : line one found
3 : AABCDEFGHI