How to find multiple line matching?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find multiple line matching?
# 1  
Old 07-11-2012
How to find multiple line matching?

Hi Unix Gurus,
I have two part of question. Any one or both answers will help me a lot.

I have a folder with many files in them. They contain SQL statement , Bind variables, Timing etc. I want to find a particular SQL(stored in one file with multiple line). I want to find that SQL in all other log files and from those file , once SQL is find then print all lines below the SQL till we find text "SQL Statement Initial Fetch Time"(including this line). Greatly appreciate your help.

to reiterate
1. store sql in file (say a.sql). Find this sql in all *.log files in folder.
2. once sql find then in that file print lines below that sql till we reach string "SQL Statement Initial Fetch Time"

Thank you.
# 2  
Old 07-11-2012
please specify with sample data
# 3  
Old 07-12-2012
With sample data

Raj_srini thanks for looking into.

The actual SQL statement could be 100 line longer but I'll prepare smaller sample file.

say input sql file a.sql is as below
Code:
select
a,
b,
c
from my_table
where a=:1 ;

I have lot of log files with various sql in them. They may contains above 6lines (say one of following log b.sql has).
Code:
loglevel is 1 for file a
log start time is xyz
select
a,
b,
c
from my_table
where a=:1;
objmgr bind :1 = abc
SQL execute time 1.25sec
SQL fetch time 2.25 sec

so output should be(from file b)
Code:
objmgr bind :1 = abc
SQL execute time 1.25sec
SQL fetch time 2.25 sec

There may be c.log, d.log file in same folder. But may not contain above SQL statement so no output from them , but let's say e.log has same sql and few lines then out may need to add

Code:
objmgr bind :1 = def
 SQL execute time 2.25sec
 SQL fetch time 3.25 sec

Basically , I'll get few line below sql statement from each file. containg the sql statement challenge I have is

1. How to match full sql string(e.g above a.sql has 6 line total between select to Smilie. How to match this multiple line in all files in folder.
2. once above step 1 is found then how to get all line between sql ending(Smilie till word Fetch time comes up from each file.

Thanks

Moderator's Comments:
Mod Comment Code tags for code, please.


---------- Post updated 07-12-12 at 03:16 AM ---------- Previous update was 07-11-12 at 01:44 PM ----------

Hi,
if any one can help with either point 1 or 2 or both then it will be a great help.

Thanks
Ranjit

Last edited by Corona688; 07-11-2012 at 03:01 PM..
# 4  
Old 07-12-2012
try this
Code:
awk 'BEGIN { flag=0;i=0;while ( getline < "a.sql" ) { gsub(" ","",$0);arr[i]=$0;i++ } }{x=$0;gsub(" ","",x);;if(x==arr[0]&&flag==0){flag=1;j=1;next};
if(flag==1){if(x==arr[j]&&j<i){j++;next}else{if(j==i){print $0;if($2~/fetch/){flag=0}}else{flag=0}}}}' b.sql

output will look like

objmgr bind :1 = abc
SQL execute time 1.25sec
SQL fetch time 2.25 sec
if you get problem with output give some more sample data

Last edited by Scrutinizer; 07-13-2012 at 10:25 AM.. Reason: code tags
# 5  
Old 07-13-2012
Getting Error

I am getting below error when trying

Code:
$ awk 'BEGIN { flag=0;i=0;while ( getline < "a.sql" ) { gsub(" ","",$0);arr[i]=$0;i++ } }{x=$0;gsub(" ","",x);;if(x==arr[0]&&flag==0){flag=1;j=1;next};
> if(flag==1){if(x==arr[j]&&j<i){j++;next}else{if(j==i){print $0;if($2~/fetch/){flag=0}}else{flag=0}}}}' b.sql
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: illegal statement near line 1


Thanks

Last edited by Scrutinizer; 07-13-2012 at 10:26 AM.. Reason: code tags
# 6  
Old 07-13-2012
who have > in the second line remove this and run
if still getting problem then send more sample data as i have tested this with data you have provided
# 7  
Old 07-13-2012
It is still giving the error

Raj,
Thanks for taking time to look into.

There is no ">" sign(it came as new line ). So I tried putting thiscommand in file and then execute file. But same error.

Code:
$ cat t1
awk 'BEGIN { flag=0;i=0;while ( getline < "a.sql" ) { gsub(" ","",$0);arr[i]=$0;i++ } }{x=$0;gsub(" ","",x);;if(x==arr[0]&&flag==0){flag=1;j=1;next};
if(flag==1){if(x==arr[j]&&j<i){j++;next}else{if(j==i){print $0;if($2~/fetch/){flag=0}}else{flag=0}}}}' b.sql

$ sh t1
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: illegal statement near line 1

Thanks

---------- Post updated at 09:24 AM ---------- Previous update was at 09:09 AM ----------

I think error may be due to I am using the solaris and that might not be understanding the gsub in awk ?

Thanks

Last edited by Scrutinizer; 07-13-2012 at 10:26 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed find multiple expressions plus next next line

Hello I am trying to write sed code where it will look through the text for lines with specific expression "Started by user" and when found, will also add the following line, and also lines with another expression "Finished:" sed -n '/Started by user/, +1p, /Finished:/'... (4 Replies)
Discussion started by: dlesny
4 Replies

2. Shell Programming and Scripting

Group Multiple Lines on SINGLE line matching pattern

Hi Guys, I am trying to format my csv file. When I spool the file using sqlplus the single row output is wrapped on three lines. Somehow I managed to format that file and finally i am trying to make the multiple line on single line. The below command is working fine but I need to pass the... (3 Replies)
Discussion started by: RJSKR28
3 Replies

3. Shell Programming and Scripting

Find files not matching multiple patterns and then delete anything older than 10 days

Hi, I have multiple files in my log folder. e.g: a_m1.log b_1.log c_1.log d_1.log b_2.log c_2.log d_2.log e_m1.log a_m2.log e_m2.log I need to keep latest 10 instances of each file. I can write multiple find commands but looking if it is possible in one line. m file are monthly... (4 Replies)
Discussion started by: wahi80
4 Replies

4. Shell Programming and Scripting

Multiple pattern find and delete line

I have a file # cat /tmp/user_find.txt /home/user/bad_link1 /home/user/www /home/user/mail /home/user/access_logs /home/user/bad_link2 I need to delete lines where there are patterns /home/user/www, /home/user/mail and /home/user/access_logs. I used below method, but its throwing error... (8 Replies)
Discussion started by: anil510
8 Replies

5. UNIX for Dummies Questions & Answers

Find string multiple times, same line

Hi everybody, Fairly simple question here: I need an awk, sed, or grep command that will find the same string multiple times on one line needs to return all lines which contain .02 twice. I do know the exact number of characters in between the two occurrences of .02 if that helps, all... (7 Replies)
Discussion started by: jgrosecl
7 Replies

6. Shell Programming and Scripting

Need to find a multiple line block and replace with a multiple line block

I would perfer to use cut and paste to do this but I can't find a GUI to do this with. What I want to do is to find a multiple line block of code like Exit Sub Log_Handler: then replace it with GoTo RSLogRtn Exit Sub Log_Handler: Basically it is just an insert, but I may want to... (8 Replies)
Discussion started by: Randem
8 Replies

7. Shell Programming and Scripting

find out line number of matching string using grep

Hi all, I want to display line number for matching string in a file. can anyone please help me. I used grep -n "ABC" file so it displays 6 ABC. But i only want to have line number,i don't want that it should prefix matching context with line number. Actually my original... (10 Replies)
Discussion started by: sarbjit
10 Replies

8. Shell Programming and Scripting

sed find matching pattern delete next line

trying to use sed in finding a matching pattern in a file then deleting the next line only .. pattern --> <ad-content> I tried this but it results are not what I wish sed '/<ad-content>/{N;d;}' akv.xml > akv5.xml ex, <Celebrant2First>Mickey</Celebrant2First> <ad-content> Minnie... (2 Replies)
Discussion started by: aveitas
2 Replies

9. Shell Programming and Scripting

find -regex: matching multiple extensions

I want to use find to locate files with two different extensions, and run a grep on the results. The closest I have gotten is incredibly slow and ugly: for i in `ls -laR|egrep -e '(.js|.css)'`; do find . -name $i -print|xargs grep -H searchBg; done; This method makes my eyes bleed. Help! ;) ... (2 Replies)
Discussion started by: r0sc0
2 Replies

10. Shell Programming and Scripting

matching multiple times in same line

Hi, I am stuck with pattern matching. I need to match a particular pattern several times in a same line and replace them. for ex., I need to convert (abc XY) (bvf ZY) bla bla to XY ZY bla bla I tried.. s/\(+ (.+)\)/$1/gi and it works (2 Replies)
Discussion started by: oldtrash
2 Replies
Login or Register to Ask a Question