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

Shlomi Fish shlomif at iglu.org.il
Wed Sep 30 09:17:57 PDT 2009


On Wednesday 30 Sep 2009 17:39:14 Madison Kelly wrote:
> Hi all,
> 
>    Thus far, I've opened a file, read it in, shifted/ignored the first X
> number of line and then wrote out the resulting file. This works, but
> strikes me as horribly inefficient. Doubly so when the file could be
> larger than RAM.
> 
>    So then, is there a more efficient/faster way of saying "Delete the
> first X number of lines from file foo"?
> 

What I would do in pure-Perl is: (untested)

<<<<<<<<<<<<<<<
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

>   Thanks all!
> 
> Madi
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm
> 

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Interview with Ben Collins-Sussman - http://shlom.in/sussman

Chuck Norris read the entire English Wikipedia in 24 hours. Twice.


More information about the toronto-pm mailing list