in reply to Removing Lines from a file

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

Replies are listed 'Best First'.
Re^2: Removing Lines from a file
by cephas (Pilgrim) on Oct 18, 2006 at 17:33 UTC
    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