[tpm] Fast(er) way of deleting X lines from start of a file

Madison Kelly linux at alteeve.com
Thu Oct 1 18:07:16 PDT 2009


Shlomi Fish wrote:
> my $num_lines_to_del = shift(@ARGV);
> my $filename = shift(@ARGV);
> # Maybe use File::Temp here
> my $temp_fn = $filename.".new";
> 
> open my $in_fh, "<", $filename
> 	or die "Could not open '$filename' - $! !";
> 
> 
> open my $temp_out_fh, ">", $temp_fn
> 	or die "Could not open temp filename - $!";
> 
> foreach my $i (1 .. $num_lines_to_del)
> {
> 	# Read one line.
> 	scalar(<$in_fh>);
> }
> 
> my $buf_len = 1024 * 16;
> 
> my $buffer;
> while (read($in_fh, $buffer, $buf_len))
> {
> 	print {$temp_out_fh} $buffer;
> }
> close($temp_out_fh);
> close($in_fh);
> 
> rename($temp_fn, $filename);
> 
> Like I said - untested, but I hope you get the idea.
> 
> Regards,
> 
> 	Shlomi Fish

I've decided to try this solution, and it works great! I'm still waiting 
to test it against very large files (> 1-2GB), but so far it seems very 
quick.

Thank you again!

Madi


More information about the toronto-pm mailing list