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. Advertisements

  2. : 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
     
    Walter Roberson, Feb 2, 2004
    #2
    1. Advertisements

  3. Nikita Synytskyy

    J Krugman Guest

    ^

    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. 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

    use -n instead of -p
    perl -wni -e "print unless /foo/" filename.ext

    the -p is just like -n with implicit print

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.