open( IP_FH, "test.txt" ); while () { if ( /ABCD/ ... /EFGH/ ) { $match = 1; $found .= $_; } else { if ( $match ) { $match = 0 ; print "========\n"; print $found; $found = (); } } } #### ABCD 122132 12 312 EFGH FRED ABCD sadasd asd das EFGH FRED ABCD asdasd d as EFGH #### ABCD 122132 12 312 EFGH ABCD sadasd asd das EFGH ABCD asdasd d as EFGH