Searching for multiple patterns in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching for multiple patterns in a file
# 1  
Old 01-10-2011
MySQL Searching for multiple patterns in a file

Hi All,

I have a file in which i have to search for a pattern from the beginning of the file and if the pattern is found , then i have to perform a reverse search from that line to the beginning of the file to get the first occurrence of another pattern.

sample input file
Code:
hey
what are you doing
IM324
A1
iam doing mca
how about you?
IM326
B2

as like mentioned above, the files contains a lots of patterns like this.
for example i have the patttern "B2" as first pattern and if that is found , i have to get first occurence of "IM..." which is associated with it. This IM pattern may be immediately above B2 or a few lines above too

if you people have some idea, Please share with meSmilie
# 2  
Old 01-10-2011
Code:
awk '$0 ~ /^IM/ {f=$0;next} $0 ~ /^B2/ {if(f){f=f"\n"$0;print f;f="";next}} {if(f) f=f"\n"$0;}' inputFile

# 3  
Old 01-10-2011
Code:
sed -n '/IM/h;/B2/{g;p;}' infile

# 4  
Old 01-10-2011
@Scrutinizer, I guess
Code:
sed -n '/IM/h;/B2/{H;g;p;}' infile

To print line having last pattern too.
# 5  
Old 01-10-2011
That is correct, although I did not gather that this is required..
# 6  
Old 01-10-2011
OR may be like this:
Code:
sed -n 'H;/IM/h;/B2/{g;p;}' infile

as above one will not print lines between patterns (as shown below). The only thing with this is that, there has to be starting pattern, otherwise (if starting pattern is not found) everything upto Ending pattern will be printed
Code:
hey
what are you doing
IM324
A1
iam doing mca
how about you?
IM326
dfgdgfdg
dfgdfgfdg
B2

# 7  
Old 01-10-2011
MySQL Searching for multiple patterns in a file

Quote:
Originally Posted by anurag.singh
@Scrutinizer, I guess
Code:
sed -n '/IM/h;/B2/{H;g;p;}' infile

To print line having last pattern too.
Hi Anurag,

Could you please explain how it works?Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching multiple patterns and construct SQL

Hello, I have attached 2 files 1) Original_table_definition.txt => which has definition of 3 tables 2) describe_table_output.txt => which has again 3 tables definition gotten thorugh doing a show table or describe table way. Now difference between 3 tables are that tablea has no... (2 Replies)
Discussion started by: nv186000
2 Replies

2. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

3. Shell Programming and Scripting

Find common patterns in multiple file

Hi, I need help to find patterns that are common or matched in a specified column in multiple files. File1.txt ID1 555 ID23 8857 ID4 4454 ID05 555 File2.txt ID74 4454 ID96 555 ID322 4454 (4 Replies)
Discussion started by: redse171
4 Replies

4. Shell Programming and Scripting

Searching multiple patterns using awk

Hello, I have the following input file: qh1adm 20130710111201 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ qh1adm 20130711151154 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ qx1adm 20130711151154 : tp count QX1 u6 -Dsourcesystems=B17,E17,EE7 qh1adm 20130711151155 : tp import all... (7 Replies)
Discussion started by: kcboy
7 Replies

5. Shell Programming and Scripting

searching multiple patterns in perl

Hi, I have code like: Output it is comming as: Rels: WM2 Rels: WG2 Rels: 5 - pre/prods.pl Rels: 6 Rels: 7 Rels: 8 Rels: 10 Rels: Int But i want only "Rels: 5" pattern Just above "- pre/prods.pl". By... (7 Replies)
Discussion started by: Anjan1
7 Replies

6. Shell Programming and Scripting

gzgrep for multiple patterns or with pattern file

Hi, I'm struggling to find an option for gzgrep to grep compressed files for multiple patterns preferanly using a pattern file (so pattern file can be altered seperately to rest of script). My regex patterns are say: 4\{15\} 3\{13\} /usr/xpg4/bin/grep will accept -f option but... (4 Replies)
Discussion started by: andyatit
4 Replies

7. Shell Programming and Scripting

Searching for multiple patterns in files

I have a situation where I need to search for multiple strings (error messages) such as 'aborted' 'file not found' etc in directory having logs. I have put all the error messages in a text file and using the command. grep -f <textfile> <filetobegrepped> I'm doing this thru a script where I... (5 Replies)
Discussion started by: bornon2303
5 Replies

8. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

9. UNIX for Dummies Questions & Answers

searching for two or more patterns in a line

how can I search for two or more patterns in one line using grep? for example if I want to show only the lines that have patterns "abc" and "123"? and what if I want to show only the lines that have either "abc" or "123" in them? any hint apprecited (4 Replies)
Discussion started by: metalwarrior
4 Replies

10. Shell Programming and Scripting

How to cut multiple patterns from a file?

Hi, I need to cut values after searching for similar patterns in a file. For example, I have the following pattern in a file: ####<Nov12 2007> <user: Vijay> <user id:123456 college:anna univ> <error code: runtime exception> I need the values for date: User: User id: College:... (5 Replies)
Discussion started by: Vijay06
5 Replies
Login or Register to Ask a Question