my $buffer; while (<$fh>) { $buffer = $_ if /$regex1/; print $buffer if /$regex2/; }