my $found; while (<>) { my $i = /ABCD/ ... /EFGH/; if ($i) { $found .= $_; if ($i =~ /E/) { print "========\n"; print $found; $found = ''; } } } #### while (<>) { my $i = /ABCD/ ... /EFGH/; print "========\n" if $i == 1; print if $i; }