in reply to Re: need to extract top and bottom lines...
in thread need to extract top and bottom lines...

how about a one-liner? perl -ne "print qq($last\n$_) if /ERROR/; $last=$_" logfile>csvfile
  • Comment on Re^2: need to extract top and bottom lines...

Replies are listed 'Best First'.
Re^3: need to extract top and bottom lines...
by johngg (Canon) on Dec 16, 2004 at 10:11 UTC
    You can add a little logic to the one-liner to cope with more general cases where some ERROR messages don't span two lines (assuming messages all start with a timestamp).

    perl -ne 'sub BEGIN {$l="";}$l=$_, next unless /ERROR/;print /^[^12]/ +? "$l$_" : $_;$l=$_;' some.log

    Not Y3K compliant.

    JohnGG

    Note: something is eating the square brackets around the character class ^12 for some reason. Code tags don't help.

      Oops, something ate the square brackets around the negated character class ^12. Maybe code tags will help, maybe not.

      perl -ne 'sub BEGIN {$l="";}$l=$_, next unless /ERROR/;print /^[^12]/ +? "$l$_" : $_;$l=$_;' some.log

      Johngg