open( IP_FH, "test.txt" ); while () { if ( /ABCD/ ... /EFGH/ ) { $match = 1; $found .= $_; } else { if ( $match ) { $match = 0 ; print "========\n"; print $found; $found = (); } } }