Deleting lines matching regexp.

Discussion in 'Perl Misc' started by Nikita Synytskyy, Feb 2, 2004.

  1. I feel this should be so obvious, but it isn't...

    From the command line, I'm trying to delete all lines matching a
    particular regexp. I know how to do a substitution:

    perl -wpi -e "s/foo/bar/g" filename.ext

    Now, how do I delete all lines that contain "foo"?

    Nikita.
    Nikita Synytskyy, Feb 2, 2004
    #1
    1. Advertising

  2. In article <bvm732$tj930$-berlin.de>,
    Nikita Synytskyy <> wrote:
    :I feel this should be so obvious, but it isn't...

    : From the command line, I'm trying to delete all lines matching a
    :particular regexp. I know how to do a substitution:

    :perl -wpi -e "s/foo/bar/g" filename.ext

    :Now, how do I delete all lines that contain "foo"?

    I swear essentially the same question was asked not more than 2 days ago...

    perl -wni -e 'print unless /foo/" filename.ext
    --
    Warning: potentially contains traces of nuts.
    Walter Roberson, Feb 2, 2004
    #2
    1. Advertising

  3. Nikita Synytskyy

    J Krugman Guest

    In <bvm7u8$83m$> -cnrc.gc.ca (Walter Roberson) writes:

    >perl -wni -e 'print unless /foo/" filename.ext

    ^

    Minor typo there.

    Also, to the OP I would advise to use the variant

    perl -i.bak -wne 'print unless /foo/' filename.ext

    so that, in case your command-line Perl doesn't attain its customary
    brilliance, you will be able to try again.

    jill
    J Krugman, Feb 2, 2004
    #3
  4. Walter Roberson wrote:

    > :Now, how do I delete all lines that contain "foo"?
    >
    > I swear essentially the same question was asked not more than 2 days ago...
    >
    > perl -wni -e 'print unless /foo/" filename.ext


    I can believe it, too! It's obvious if you know the answer, otherwise
    you'll spend a day searching.

    Thanks for the speedy reply.

    Nikita.
    Nikita Synytskyy, Feb 2, 2004
    #4
  5. Nikita Synytskyy

    gnari Guest

    "Nikita Synytskyy" <> wrote in message
    news:bvm732$tj930$-berlin.de...
    > I feel this should be so obvious, but it isn't...
    >
    > From the command line, I'm trying to delete all lines matching a
    > particular regexp. I know how to do a substitution:
    >
    > perl -wpi -e "s/foo/bar/g" filename.ext


    use -n instead of -p

    > Now, how do I delete all lines that contain "foo"?


    perl -wni -e "print unless /foo/" filename.ext

    the -p is just like -n with implicit print

    gnari
    gnari, Feb 2, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harry Barker
    Replies:
    2
    Views:
    505
    Alf P. Steinbach
    Apr 19, 2006
  2. Joao Silva
    Replies:
    16
    Views:
    340
    7stud --
    Aug 21, 2009
  3. lcs Mixmaster Remailer

    Matching multiple lines with regexp

    lcs Mixmaster Remailer, Feb 11, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    82
    Tad McClellan
    Feb 11, 2004
  4. Bobby Chamness
    Replies:
    2
    Views:
    214
    Xicheng Jia
    May 3, 2007
  5. crea
    Replies:
    2
    Views:
    397
    Nobody
    Dec 28, 2012
Loading...

Share This Page