Extracting text between two patterns 1 and 2 and pattern2 should be second occurrence of the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting text between two patterns 1 and 2 and pattern2 should be second occurrence of the file
# 1  
Old 08-16-2011
Extracting text between two patterns 1 and 2 and pattern2 should be second occurrence of the file

Hi All,
I have a small query. I have a file containing the following lines

File 1:
29-Jul-2011 [pattern1] GMT Static data requires update <Extraction should start here>
-----------
-----------
----------[pattern2]----------
-----------------------
-----------[pattern2] <should stop here>


Pattern1 will be time and pattern2 will be some userid:XYZ

I am trying to use following script, but it stops after the first occurrence of pattern2
awk ' /08:48:17/ { flag=1; } /gangii87/{ print $0; flag=0;} flag { print $0 }' log


Thanks
# 2  
Old 08-16-2011
Quote:
Originally Posted by gangii87
Hi All,
I have a small query. I have a file containing the following lines

File 1:
29-Jul-2011 [pattern1] GMT Static data requires update <Extraction should start here>
-----------
-----------
----------[pattern2]----------
-----------------------
-----------[pattern2] <should stop here>


Pattern1 will be time and pattern2 will be some userid:XYZ

I am trying to use following script, but it stops after the first occurrence of pattern2
awk ' /08:48:17/ { flag=1; } /gangii87/{ print $0; flag=0;} flag { print $0 }' log


Thanks
Hi, It could really help if you give a real input ..
# 3  
Old 08-17-2011
Hi
Please find the exact logs below

logs:
29-Jul-2011 08:48:22,653 GMT Static data requires update
serverID=fxValue,CancelTimestamp=0,createdTimestamp=1311925697809,lastUpdatedBy=null,revision=0]
Location=London,Name=IBM London Operation Vincenzo Maini,cancelReason=null,ndf=false,
29-Jul-2011 08:48:23,653 GMT Static data requires update
serverID=fxValue,CancelTimestamp=0,createdTimestamp=6311925697809,lastUpdatedBy=null,revision=0]
Location=London,Name=IBM London Operation Vincenzo Maini,cancelReason=null,ndf=false,
createdByUserId=sam@gmail.com
------------------------
-------------------------
-------------------------
-------------------------
29-Jul-2011 08:48:24,653 serverID=fxValue,CancelTimestamp=0,createdTimestamp=8311925697809, lastUpdatedBy=null,revision=0] Location=London,Name=IBM London Operation Vincenzo Ludovico Maini,cancelReason=null,ndf=false,book=LEMFXAUT, createdByUserId=sam@gmail.com


In the above logs my search pattern will be 08:48:22 and UserId=sam@gmail.com

---------- Post updated 08-17-11 at 03:05 PM ---------- Previous update was 08-16-11 at 04:38 PM ----------

Hi All,
I finally got the logic by reading few older posts in this site
really this site is very helpful for the beginners like me

Here is working logic
awk '
BEGIN { start=0; skip=0; }
{
if(index($0, "08:48:22")> 0)
{start=1; print $0}
else
if(index($0, "sam@gmail.com")> 0) { if(skip==0) skip=1; else {skip=0; start=0; print$0 }}
else
if(start==1)
{
print $0
}
} ' log

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

2. Shell Programming and Scripting

Adding text to the end of the specific line in a file(only to the first occurrence of it)

Hi, I want to add a text to the end of the specific line in a file. Now my file looks like this: 999 111 222 333 111 444 I want to add the string " 555" to the end of the first line contaning 111. Moreover, I want to insert a newline after this line containg the "000" string. The... (8 Replies)
Discussion started by: wenclu
8 Replies

3. Shell Programming and Scripting

Extracting a set of patterns from the text file

Hi experts, I need a help in extracting a set of patterns from the text file. Below is my scenario. Input file: I need to extract the data between My output should be as Thanks, Kalai (7 Replies)
Discussion started by: kalpeer
7 Replies

4. Shell Programming and Scripting

Append text to line if begins with pattern1 AND does not end with pattern2

Hello, I'm looking for sed solution to change ... <li>keyword</li> <li>keyword <li>keyword</li> <li>keyword <li>keyword</li> ... to ... <li>keyword</li> <li>keyword</li> <li>keyword</li> <li>keyword</li> <li>keyword</li> ... I.e., if lines beginning with <li> do not end with... (3 Replies)
Discussion started by: pioavi
3 Replies

5. Shell Programming and Scripting

[Solved] Sed/awk print between patterns the first occurrence

Guys, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla bla 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla ... (6 Replies)
Discussion started by: ppolianidis
6 Replies

6. Shell Programming and Scripting

Sed/awk print between different patterns the first occurrence

Thanks for the help yesterday. I have a little modification today, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR Exception1 blablabla bla bla bla bla 2011-08-14... (2 Replies)
Discussion started by: ppolianidis
2 Replies

7. UNIX for Dummies Questions & Answers

Locating and Extracting Specific Patterns from a file

Hi all, 1. I have a file that is getting continously refreshed (appended) I want to grep all the strings containing substring of the type abcdf123@aaa.xxx.yyy.zzz:portnumber: where, before @, any letters or numbers combination, after @, IP address then symbol : then port... (4 Replies)
Discussion started by: kokoras
4 Replies

8. Shell Programming and Scripting

Sed Help.To Search Between Pattern1 And Pattern2 Containing Certain Text

Hi, Here is a sample of my Test File $ cat TestFile1 Prompt Table DQZ_ALTER_SCHEMA_ID; ALTER TABLE DQZ.DQZ_ALTER_SCHEMA_ID MONITORING; ALTER TABLE DQZ.DQZ_ALTER_SCHEMA_ID STORAGE ( NEXT 3464K ); Prompt Table DQZ_ALTER_SCHEMA_ID; ALTER TABLE DQZ.DQZ_ALTER_SCHEMA_ID MOVE LOB... (16 Replies)
Discussion started by: rajan_san
16 Replies

9. Shell Programming and Scripting

Getting the lines between last occurrence of two patterns

Hi I am new shell scripting, i need help on the follwoing I have a application log file, the application is called on cron, the log includes a "started" and "finished" lines repeatedly. I need to get the log of the for the latest run of the application. Sample log file Started adfa fadfa... (8 Replies)
Discussion started by: rvz
8 Replies

10. UNIX for Dummies Questions & Answers

delete string in a text file leaving the first occurrence

Hi, How can i delete the second and subsequent occurrence of a particular string from a file ? eg) test.txt cattle bat battle mat matter cattle cattle my output file should be cattle bat battle mat matter (12 Replies)
Discussion started by: gopskrish
12 Replies
Login or Register to Ask a Question