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

Madison Kelly linux at alteeve.com
Thu Oct 1 08:40:04 PDT 2009


Antonio Sun wrote:
> 
> 
> On Wed, Sep 30, 2009 at 11:57 AM, Alex Beamish <talexb at gmail.com 
> <mailto:talexb at gmail.com>> wrote:
> 
>      >  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
> 
> 
> If using shell script instead of Perl is an option, then  sed is the 
> best tool
> 
>   ToDel=20
>   sed -i "1,${ToDel}d" file
> 
> will delete the first 20 lines from the file.
> 
> HTH
> 
> Antonio

Hrm, that looks interesting, and is only one system call. Maybe I'll add 
it as an optional way to cut the first few lines off for people on 
systems with 'sed'. Regardless, I will test it on some large files to 
see how much faster it is.

Thanks

Madi



More information about the toronto-pm mailing list