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

Viktor Pavlenko vvp at cogeco.ca
Thu Oct 1 18:53:52 PDT 2009


>>>>> "MK" == Madison Kelly <linux at alteeve.com> writes:

    >> perl -i -ne 'print if $. > 20' file

    MK> Where 'if' is the input file handle? I've not played with
    MK> '$.'...

$. is the line number, starting from 1. `if' is just an `if', you can
write it traditional way: if ($. > 20) { print }. It says to print $_
(omitted) if the line number is greater than 20. It's a perl
one-liner, it allows you to read one line at a time and put it in $_,
-n means don't print it, and -i tells perl to edit the file in place
(all tmp file business is taken care of). What follows -e is the
script to execute.

    MK> 'If this works it'd be the pure-perl way I was hoping
    MK> for. Do you know if this still reads the whole file into
    MK> memory at once? My biggest concern, even more than speed, is
    MK> that this may need to be done on files larger than available
    MK> RAM.

I'm sure it reads a line at a time and then throws it away, so no
worries about memory. Your point about inefficiency of reading &
copying remains valid.

HTH

-- 
Viktor



More information about the toronto-pm mailing list