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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep pattern from different file and display if it exists in the required file
# 1  
Old 12-07-2010
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 the line1 which is suppose to be grepped and displayed as a output."

My requirement 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.

Can somebody help me on getting this.

Thanks.
# 2  
Old 12-07-2010
Code:
grep -f xxx.txt yyy.txt
fgrep xxx.txt yyy.txt

# 3  
Old 12-07-2010
Code:
awk -F \" 'NR==FNR {a[$2];next} {for (i in a) if ($0 ~ i) print }'  xxx.txt yyy.txt

# 4  
Old 12-08-2010
Now i am getting the line which has the required pattern.

Whenever the pattern is matched, it should display the content from other file.

Eg:

I have three files say xxx.txt, yyy.txt, and zzz.txt
xxx.txt 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."

zzz.txt with the following lines. eg:
"line one found"
"line two found"

My requirement is to search the pattern given in xxx.txt at yyy.txt and if it exists in yyy.txt then display the corresponding line from zzz.txt.

Eg output: line one found

Can you please let me know the solution?

Last edited by abinash; 01-15-2011 at 01:44 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If file pattern exists in directory then continue

he below looks in $dir for any pattern of fileone. As is, it executes but only returns File found if the exact format in the script exsists. Why isn't a pattern of fileone being looked for and if it is in $dir, File found. I think that is what should happen. Thank you :). dir=/path/to if... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Search if file exists for a file pattern stored in array

Hi experts, I have two arrays one has the file paths to be searched in , and the other has the files to be serached.For eg searchfile.dat will have abc303 xyz123 i have to search for files that could be abc303*.dat or for that matter any extension . abc303*.dat.gz The following code... (2 Replies)
Discussion started by: 100bees
2 Replies

3. UNIX for Dummies Questions & Answers

Outputting 1 file per row if pattern exists between files

I have many files that can have various amounts of rows. I essentially want to output each row into a new file if a pattern is matched between two files. I have some code that does something similar but I want it to output every single input row from every file into a separate output file; that... (5 Replies)
Discussion started by: verse123
5 Replies

4. Shell Programming and Scripting

Refrain the Message of File Not exists display out

Hi All, Would like to ask on how to refrain the message file not exists from display out. if ; then When it execute, the OS will throw the error file does not exists ls: 0653-341 The file COL_*/*.CTL does not exist. Thanks. (3 Replies)
Discussion started by: ckwan
3 Replies

5. Shell Programming and Scripting

Check for Pattern if exists write to file

Hi ! All I just want to search and write to new file if pattern is found in text file following are my text files by which I want to search Month and last column number my text file1 15-Jan-2011 25 ARTS 1255 125 125 178 198 15-Jan-2011 25 ARTS 1255 125 125 178 198 15-Jan-2011 25... (3 Replies)
Discussion started by: nex_asp
3 Replies

6. UNIX for Dummies Questions & Answers

Need help finding a file where a pattern exists and the file has a timestamp

So, I know how to do some of this stuff on an individual level, but I'm drawing a blank as to how to put it all together. I have a pattern that I'm looking for in a log file. The log file I know came in yesterday, so I want to limit the search to that day's listing of files. How would I do... (5 Replies)
Discussion started by: kontrol
5 Replies

7. Shell Programming and Scripting

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

8. Shell Programming and Scripting

Want to grep for a pattern and display the content above that pattern

Hi, When we have a failure, sometimes we just step restart the job from the next step. Later when we open the log for analysis of the failure, it is becoming difficult to go to the failure part. For eg., if it is a 1000 line log, the failure may be at 500th line. so wat i want to do is, grep... (6 Replies)
Discussion started by: ajayakunuri
6 Replies

9. Shell Programming and Scripting

check if file exists with pattern matching

Hello friends, I am writing a simple shell script which will copy one particular type of files to backup folder if files exists. If files doesn't exists, mv command should not be executed. My file pattern is like wcm-spider-maestro.log.2009-07-15, wcm-spider-maestro.log.2009-07-16 etc.. I... (6 Replies)
Discussion started by: sreenu.shell
6 Replies

10. UNIX for Dummies Questions & Answers

grep required pattern and next 2 or 3 lines

dear ones pl.kindly help me 1) how to print(grep) required pattern and following 2 or 3 lines. 2) grep required pattern(to print)+above 2 lines+below 2 or 3 lines.from a report file. ex: we have some report file kf askfjsk fksaj fk skf sjfksjd kff sjfkjs kf jskdjfklsd jfklsdf sdkfjsd fsd... (3 Replies)
Discussion started by: cvvsnm
3 Replies
Login or Register to Ask a Question