[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
      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 :

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

Please help!

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:

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

thanks a lot, that's exactly what i needed!

Alternate solution..

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.

Hi,

Using 'sed':

$ 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

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.