my @cache; my $found=0; while (<$file>) { if ( /stringtobefound/) { $found++; } if (//) { @cache=(); } push(@cache, $_); # print "-----------\nFound is $found, Cache is\n".@cache."--------------"; if (/<\/end of record>/ and $found) { print @cache; $found=0; } }