Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

perl -pie "???"

by Plankton (Vicar)
on Aug 17, 2004 at 21:21 UTC ( [id://383798]=perlquestion: print w/replies, xml ) Need Help??

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

How would I write a perl -pie '???' one liner to delete the first line of a file?

Plankton: 1% Evil, 99% Hot Gas.

Replies are listed 'Best First'.
Re: perl -pie "???"
by BrowserUk (Patriarch) on Aug 17, 2004 at 21:55 UTC

    perl -pi.bak -e"BEGIN{<>}" file ## Or perl -pi -e 'BEGIN{<>}' file

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
•Re: perl -pie "???"
by merlyn (Sage) on Aug 17, 2004 at 22:34 UTC
      perl -0777 -pe's/^.*\n//' any number of files

      This won't work correctly on files with a single line and no terminating newline, though. If that is a concern, the following is necessary:

      perl -0777 -pe's/^.*(?:\n|\z)//' any number of files

      I really wish there was a metacharacter for "either newline or end of string" as the counterpart to $ — this isn't the first time it would have been handy.

      Makeshifts last the longest.

Re: perl -pie "???"
by diotalevi (Canon) on Aug 17, 2004 at 21:23 UTC
    perl -nie 'print if $. > 1' # Better perl -ie '<>; print while <>'

    Oops. I don't use -i enough to realize that the following text is an argument for it.

    perl -ibak -n -e 'print if $. > 1' # Better perl -ibak -e '<>; print while <>'

      In line with recent discussions of the flip-flop operator, here's the flipside:

      perl -ni -e 'print if 2 .. 0' file

      Hugo

        golf!
        2..!print
      Hmmm it must be how I am invoking perl.

      Here's the line I want to get rid of ...
      $ head -1 DAR_RH9_install/index.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:/ +/www.w3.org/TR/html4/loose.dtd">
      Here's what I tried ...
      $ perl -nie 'print if $. > 1' DAR_RH9_install/index.html Can't open perl script "print if $. > 1": No such file or directory $ perl -ie '<>; print while <>' DAR_RH9_install/index.html Can't open perl script "<>; print while <>": No such file or directory
      What the heck I am doing wrong here?

      Plankton: 1% Evil, 99% Hot Gas.
        Perl isn't seeing the -e switch. Anything immediately following the -i is assumed to be the backup extension, ie "perl -i.bak..." will create a backup named filename.bak and operate on the original.

        Use perl -i -e '...' (or -i.bak -e if you want a backup).

        The -i switch takes an optional backup extension, so you can no longer combine it with other following option flags in modern perls. Write it instead as:

        perl -ni -e '...' file

        Hugo

        Change perl -nie ... to perl -ni -e .... -i accepts an optional argument, so a space is needed after it.
        perl -ni -e 'print if $. > 1' filename
Re: perl -pie "???"
by jmcnamara (Monsignor) on Aug 17, 2004 at 22:09 UTC

    Some variations:
    perl -i -ne 'print if $.-1' file perl -i -pe 'goto LINE if $. == 1' file perl -i -pe '$_=$+if!$if++' file

    --
    John.

Re: perl -pie "???"
by ysth (Canon) on Aug 17, 2004 at 22:11 UTC
    Please, monks, remember to spell words whenever possible. Use perl -wi -ne and perl -wi -pe. perl -li -nUx should work too.
Re: perl -pie "???"
by Chady (Priest) on Aug 18, 2004 at 05:52 UTC

    here's my take, since you want -p and not -n

    perl -pi -e '$_="" if $i++' file

    Update: oops! wrote it backwards. Thanx TZapper for pointing it out.

    perl -pi -e '$_="" unless $i++' file

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
      Chady, your one-liner deletes everything but the first line.
Re: perl -pie "???"
by hardburn (Abbot) on Aug 17, 2004 at 21:24 UTC

    Perhaps next if $. == 1?

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

      No. print occurs inside the continue block which is executed at the end of the while's block. next() just jumps right into continue.

      while ( <> ) { # YOUR CODE GOES HERE } continue { # next() resumes here. print }
Re: perl -pie "???"
by Anonymous Monk on Aug 18, 2004 at 09:22 UTC
Re: perl -pie "???"
by demerphq (Chancellor) on Aug 18, 2004 at 11:49 UTC
    perl -i.bak -pe "$_='' unless $.>1"

    the -i.bak is necessary cause you cant use i without a backup extension on Win32.


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


Re: perl -pie "???"
by Anonymous Monk on Aug 18, 2004 at 11:45 UTC
Re: perl -pie "???"
by Random_Walk (Prior) on Aug 18, 2004 at 13:23 UTC
    This is fun, here is my take on it

    perl -pi -e '$_=""if$.==1'

    cheers.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 23:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found