Solução rápida (e não testada): my (@buffer,$current_status); while (my $line = <>) { push $line, $buffer; if ($line =~ /Status:\s*(\w+)/) { $current_status = $1; } if ($line =~ /End/) { if ($current_status eq 'ERROR') { print join("\n", @buffer); } @buffer = (); } }