in reply to need to extract top and bottom lines...

The idea here is to keep a context, by storing the previous line:
open (DIFFR, "/tmp/log.diff") or die "Could not open ;/tmp/log.diff\n" +; my $prev = <DIFFR>; while (my $curr = <DIFFR>) { print "$prev$curr" if $curr =~ /ERROR/; $prev = $curr; }

-Mark

Replies are listed 'Best First'.
Re^2: need to extract top and bottom lines...
by holli (Abbot) on Dec 15, 2004 at 15:42 UTC
    how about a one-liner? perl -ne "print qq($last\n$_) if /ERROR/; $last=$_" logfile>csvfile
      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