open IP_FH, '<', 'test.txt' or die "Cannot open 'test.txt' $!"; my ( $match, $found ); while ( ) { if ( /ABCD/ ... /EFGH/ ) { $match = 1; $found .= $_; } if ( $match ) { $match = 0 ; print "========\n"; print $found; $found = ''; } }