[Solved] Sed/awk print between patterns the first occurrence | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

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

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-13-2011
ppolianidis ppolianidis is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 15 May 2012, 7:06 AM EDT
Posts: 8
Thanks: 1
Thanked 1 Time in 1 Post
[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:


Code:
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
      bla
2011-08-14 21:10:04,535 blablabla ERROR blablabla
       bla
       bla
      bla
      bla


what i need is to print everything between the ERROR pattern but only the first occurrence :


Code:
2011-08-14 21:10:04,535 blablabla ERROR blablabla
       bla
       bla
      bla
      bla


Please help!

Last edited by pludi; 09-13-2011 at 07:42 AM..
Sponsored Links
    #2  
Old 09-13-2011
sk1418 sk1418 is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 8 March 2012, 11:08 AM EST
Posts: 236
Thanks: 1
Thanked 55 Times in 54 Posts
the example you made is not so clear. timestamps are the same, also those "bla"s.

try this one liner, if it gives your needs:


Code:
awk  '/ERROR/{if(b) exit; else b=1}1'  yourFile

Sponsored Links
    #3  
Old 09-13-2011
ppolianidis ppolianidis is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 15 May 2012, 7:06 AM EDT
Posts: 8
Thanks: 1
Thanked 1 Time in 1 Post
thanks a lot, that's exactly what i needed!

Last edited by vbe; 09-13-2011 at 09:01 AM.. Reason: And so, we can put the thread as Solved...
    #4  
Old 09-14-2011
michaelrozar17's Avatar
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 17 March 2014, 1:43 AM EDT
Location: IN
Posts: 892
Thanks: 14
Thanked 182 Times in 173 Posts
Alternate solution..

Code:
awk '/ERROR/{++t}t==1' inputfile

Sponsored Links
    #5  
Old 09-14-2011
sk1418 sk1418 is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 8 March 2012, 11:08 AM EST
Posts: 236
Thanks: 1
Thanked 55 Times in 54 Posts
Quote:
Originally Posted by michaelrozar17 View Post
Alternate solution..

Code:
awk '/ERROR/{++t}t==1' inputfile

this short one-liner looks nice. however it will go through the whole file anyway after getting the part of text we needed.
Sponsored Links
    #6  
Old 09-15-2011
birei birei is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 24 June 2014, 5:36 AM EDT
Location: Bilbao, Spain
Posts: 628
Thanks: 8
Thanked 173 Times in 171 Posts
Hi,

Using 'sed':

Code:
$ cat infile
2011-08-14 21:10:04,535 blablabla ERROR Exception1 blablabla
bla
bla
bla
bla
2011-08-14 21:10:04,535 blablabla ERROR Exteption2 blablabla
bla
bla
bla
bla
2011-08-14 21:10:04,535 blablabla ERROR Exception1 blablabla
bla
bla
bla
bla
2011-08-14 21:10:04,535 blablabla ERROR Exception3 blablabla
bla
bla
bla
$ sed -ne '/ERROR/I,/ERROR/I { H; b }; x; s/^\n//; s/\(.*\)\n.*/\1/; p; q' infile
2011-08-14 21:10:04,535 blablabla ERROR Exception1 blablabla
bla
bla
bla
bla

Regards,
Birei
Sponsored Links
    #7  
Old 09-15-2011
ppolianidis ppolianidis is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 15 May 2012, 7:06 AM EDT
Posts: 8
Thanks: 1
Thanked 1 Time in 1 Post
Thanks!

---------- Post updated at 05:19 PM ---------- Previous update was at 05:12 PM ----------

The above doesnt work as expected. The Exception1 is just an example. This command will run as a script and the pattern is a variable. What i need is to separate the errors and store an example of it in a file. The errors are different so i have to use the description of the error (i.e. Exception1) as a pattern.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extracting text between two patterns 1 and 2 and pattern2 should be second occurrence of the file gangii87 Shell Programming and Scripting 2 08-17-2011 04:05 PM
Perl print between 2 patterns killer54291 Shell Programming and Scripting 10 06-27-2011 03:43 PM
Sed to print a string until the second occurrence of a character imr Shell Programming and Scripting 7 11-02-2009 05:02 AM
Getting the lines between last occurrence of two patterns rvz Shell Programming and Scripting 8 08-08-2008 05:32 PM
Print last occurrence if first field match Raynon Shell Programming and Scripting 9 03-13-2008 03:28 AM



All times are GMT -4. The time now is 05:11 PM.