Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Removing Lines from a file

by artist (Parson)
on Oct 18, 2006 at 15:13 UTC ( [id://579100]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I like to remove certain lines, based on some criteria from a single file. File::Alter doesn't cut it completely, because it doesn't the alter the file itself.
--Artist

Replies are listed 'Best First'.
Re: Removing Lines from a file
by davorg (Chancellor) on Oct 18, 2006 at 15:18 UTC

    Look at Tie::File instead. It's included with modern versions of Perl.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Removing Lines from a file
by davido (Cardinal) on Oct 18, 2006 at 15:36 UTC

    With Tie::File you get to use conveniences such as splice, push, shift, and so on.

    Another method is to open the input file, open an output file, read the input file line by line, and as you read it, write line by line to the output file, of course skipping output for those lines you wish to drop (next is great for that).

    Once you've finished reading the input and writing the output file, unlink the input file, and rename the output file to the same name as the original input file.

    That's pretty much what the -i command line switch implements behind the scenes.


    Dave

Re: Removing Lines from a file
by johngg (Canon) on Oct 18, 2006 at 16:13 UTC
    You could do this in place but using the -n switch rather than -p. The difference is there is no implicit print with -n so you can decide to print which lines you want. Like this (on *nix)

    $ cat myfile 1st line 2nd line 3rd line $ perl -ni.bak -e 'next if /^2nd/; print;' myfile $ cat myfile 1st line 3rd line $ cat myfile.bak 1st line 2nd line 3rd line $

    I hope this is of use.

    Cheers,

    JohnGG

      You can also use -p with a goto:
      perl -pe 'goto LINE if /^2nd/' myfile

      Which makes since if you look at what it turns into:
      $ perl -MO=Deparse -pe 'goto LINE if /^2nd/' myfile LINE: while (defined($_ = <ARGV>)) { goto LINE if /^2nd/; } continue { print $_; } -e syntax OK
        Well, that's something new I've learnt. Thank you. Two things in fact as I've never played with -MO=Deparse before and now I can see how useful it can be.

        The only problem with goto is personal in that my first language was Fortran IV; no code blocks and no else clauses meant that your code was full of GO TO's and I became heartily sick of them :)

        Cheers,

        JohnGG

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 22:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found