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

Hey, I know how to read the first line of a file, but how do i delete the first line of a file? Thanks for your help, Kiko

Replies are listed 'Best First'.
Re: Deleting the first line of a file
by btrott (Parson) on Apr 06, 2000 at 00:38 UTC
Re: Deleting the first line of a file
by turnstep (Parson) on Apr 06, 2000 at 00:48 UTC
    No shortcut really, you have to open the file, read in the contents, and then re-write it, minus the first line:
    open(FOO, "+< $myfile") || die "Could not open $myfile $!\n"; @bar=<FOO>; shift(@bar); seek(FOO, 0, 0); truncate(FOO, tell); print FOO @bar; close(FOO);
    If you don't want to mess around with the seeking and truncating, just open it twice:
    open(FOO, "$myfile") || die "Could not open $myfile $!\n"; @bar=<FOO>; shift(@bar); close(FOO); open(FOO, ">$myfile") || die "Could not open $myfile $!\n"; print FOO @bar; close(FOO);
Re: Deleting the first line of a file
by plaid (Chaplain) on Apr 06, 2000 at 03:17 UTC
    Just to throw out another way to do it, you could try something along the lines of
    perl -ni -e 'if (2 .. eof) { print }' <file>
    Not too different from brott's example, but it's always interesting to see different ways to do the same thing.
      And to ice the cake...
      perl -pi -e 'BEGIN{<>}' files you want to munge