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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep a pattern given in one file at other file and display its corresponding contents as output.
# 1  
Old 01-16-2011
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.
Code:
"this is the line1"
"this is the line2"

The yyy.txt with lot of lines. eg:
Code:
"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)
Code:
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.
Code:
"this is the line1"=line one found
"this is the line2"=line two found

bbb.txt with lot of lines. eg:
Code:
"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.

Last edited by Franklin52; 02-22-2011 at 03:05 AM.. Reason: Please use code tags, thank you
# 2  
Old 01-16-2011
Hi,

Modified your existing code,

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

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 01-16-2011
Awesome thanks :-)

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
# 4  
Old 01-16-2011
Tr this,

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

This User Gave Thanks to pravin27 For This Post:
# 5  
Old 02-21-2011
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.
Code:
"this is the line1"=line one found
"this is the line2"=line two found

bbb.txt with lot of lines. eg:
Code:
"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:
Code:
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.

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


Last edited by Franklin52; 02-22-2011 at 03:06 AM.. Reason: Please use code tags, thank you
# 6  
Old 02-21-2011
Try this,
Code:
 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

# 7  
Old 02-22-2011
It simply ignores the code. Not even displaying an empty line. i am not sure if i am missing something. Pls help :-(
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output file name and file contents of multiple files to a single file

I am trying to consolidate multiple information files (<hostname>.Linux.nfslist) into one file so that I can import it into Excel. I can get the file contents with cat *Linux.nfslist >> nfslist.txt. I need each line prefaced with the hostname. I am unsure how to do this. --- Post updated at... (5 Replies)
Discussion started by: Kentlee65
5 Replies

2. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

3. UNIX for Dummies Questions & Answers

HTML: Display contents of file using Variable

<tr><td bgcolor=#D7EBAD><b>Instructions :</b></td> <td>`cat temp.txt`</td></tr> Hi Experts, I have an requirement of displaying file contents in HTML mail , for which am using the above approach, Where as the output is kind of not as expected. If there are 5 lines in the file, in the... (4 Replies)
Discussion started by: scott_cog
4 Replies

4. Shell Programming and Scripting

Run a program-print parameters to output file-replace op file contents with max 4th col

Hi Friends, This is the only solution to my task. So, any help is highly appreciated. I have a file cat input1.bed chr1 100 200 abc chr1 120 300 def chr1 145 226 ghi chr2 567 600 unix Now, I have another file by name input2.bed (This file is a binary file not readable by the... (7 Replies)
Discussion started by: jacobs.smith
7 Replies

5. Shell Programming and Scripting

Help with ksh script to display output with specific contents

This is Input - starts with Storage Group Name and ends with Shareable and the loop continues all I need is Storage group name and Alu numbers in the below output format requested. Storage Group Name: abcd Storage Group UID: 00:00:000:00:0:0:0 HBA/SP Pairs: HBA UID ... (6 Replies)
Discussion started by: maddysa
6 Replies

6. Shell Programming and Scripting

Grep -v contents of a list file from another file.

Hello, I am trying to get output from FILE2 that excludes servers from list FILE1. I've tried a for loop with no success. Any ideas how to go about this? FILE1 contains: server1 server2 server3 server4 server5 FILE2 contains: server1:ERROR:user1: error message... (2 Replies)
Discussion started by: LinuxRacr
2 Replies

7. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

8. Shell Programming and Scripting

sed command for copying the contents of other file replacing it another file on specifc pattern

We have 2 file XML files - FILE1.XML and FILE2.xml - we need copy the contents of FILE1.XML and replace in FILE2.xml pattern "<assignedAttributeList></assignedAttributeList>" FILE1.XML 1. <itemList> 2. <item type="Manufactured"> 3. <resourceCode>431048</resourceCode> 4. ... (0 Replies)
Discussion started by: balrajg
0 Replies

9. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, 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... (3 Replies)
Discussion started by: abinash
3 Replies

10. Shell Programming and Scripting

Unable to display correctly the contents of a file without a line feed

I am using AIX and ksh. I need to display the contents of a file that has a pid (process id). Because the file is open, it doesn't have the line feed or new line, so for some reason if I do this: `cat $pid` , where $pid is the name of the fully qualified file, it displays test3.sh: 426110:... (1 Reply)
Discussion started by: Gato
1 Replies
Login or Register to Ask a Question