Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Deleting the first line of a file

by Kiko (Scribe)
on Apr 06, 2000 at 00:31 UTC ( #6965=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://6965]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2022-07-03 08:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?