MBolton has asked for the wisdom of the Perl Monks concerning the following question:

I am working with a file that has 32 lines in the beginning of it that I don't want to have appear in the final output. Everytime the file is created, the same 32 lines will appear so the number of lines that I need to strip out from the beginning of the file is consistent. I am somewhat familiar with how to use the File functions in Perl and I have used them in the past to filter out portions of a file that I *do* want, I just have never used it in the opposite fashion. I hope that this is somewhat coherent and makes sense.. Thanks in advance for any help anyone can provide!

Replies are listed 'Best First'.
Re: Cut off beginning lines of a file
by Fletch (Bishop) on May 19, 2005 at 18:47 UTC

    perldoc perlvar look for $., start printing if that's >= 33.

Re: Cut off beginning lines of a file
by VSarkiss (Monsignor) on May 19, 2005 at 19:16 UTC
Re: Cut off beginning lines of a file
by davidrw (Prior) on May 19, 2005 at 19:32 UTC
    Basically same as above solutions, but presented as a command-line filter:
    perl -ne 'print if $. >= 33'
Re: Cut off beginning lines of a file
by chas (Priest) on May 19, 2005 at 19:12 UTC
    You could do something like:
    { my $i; while(<>){$i++;print if $i>32;} }

    You didn't say what you wished to do with the output, but you could direct the print to a file, etc.
    chas
Re: Cut off beginning lines of a file
by sh1tn (Priest) on May 19, 2005 at 22:28 UTC
    Just skip the first 32 lines:
    ... while( <FH> ){ $. <= 32 and next; ... } ...


Re: Cut off beginning lines of a file
by BrowserUk (Patriarch) on May 19, 2005 at 21:03 UTC

    If you need to do it in-place rather than by copying the file you could use this:

    #! perl -slw use strict; open my $fh, '+<', $ARGV[ 0 ] or die $!; <$fh> while ($.||0) < 32; my $bufsize = tell( $fh ); my( $readPoint, $writePoint ) = ( $bufsize, 0 ); my $buffer; local $/; while( my $read = read( $fh, $buffer, $bufsize ) ) { seek $fh, $writePoint, 0; print $fh $buffer; $writePoint += $read; seek $fh, $readPoint+=$read, 0; } truncate $fh,$writePoint; close $fh;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.