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

Madison Kelly linux at alteeve.com
Thu Oct 1 08:36:31 PDT 2009


Alex Beamish wrote:
> On Wed, Sep 30, 2009 at 11:39 AM, Madison Kelly <linux at alteeve.com> 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"?
> 
> I would expect you could fashion something workable like this
> 
>   #  Get the number of lines in the file ..
>   wc -l file
> 
>   #  tail the desired number of lines to a new file ..
>   tail -n $numberOfLinesInFile-$numberOfLinesYouDontWant file >newfile
> 
>   #  and move the files around.
>   rm file; mv newfile file

That would work, but I am trying to/hoping to come up with a pure-perl 
solution. I'm hoping to make the module I am using this in as portable 
as possible, so I'd like to stick to core modules and functions. Perhaps 
for now, what I am doing is the only real option.

Madi



More information about the toronto-pm mailing list