open my $TEMPF, '>', 'tempfile.tmp' or die("Unable to write to tempfile"); print $TEMPF $file_contents; close $TEMPF; open my $TEMPF, '<', 'tempfile.tmp' or die("Can't read tempfile back in"); while (chomp (my $line = <$TEMPF>)) { next unless $line =~ /^06/; #skip unneeded lines chomp(my $line2 = <$TEMPF>); #process lines } close $TEMPF; unlink('tempfile.tmp');