kri54sub has asked for the wisdom of the Perl Monks concerning the following question:

I need to print out lines (from multiple files) starting from a string "Server error:" to a line above the string "server info", only if I see a pattern /Btree/ within this text block. I tried using the flip-flop operator but it did not give me the desired result. The grep -B# -A# works but the number of lines within the text block may vary. Hence the grep approach does not suit me. Can you send me your thoughts?

Replies are listed 'Best First'.
Re: Newbie : Data mining question
by jasonk (Parson) on Oct 29, 2007 at 20:21 UTC
    for ( $content =~ /^(Server error:\n.*?)^server info/gsm ) { print if /Btree/; }

    We're not surrounded, we're in a target-rich environment!

      Replacing for with while would save memory at no cost.

      Update: Oops, not quite true. It would involve copying $1 into some other variable, so it might lose speed.

      while ( $content =~ /^(Server error:\n.*?)^server info/gsm ) { my $record = $1; print $record if $record =~ /Btree/; }