Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Deleting lines matching regexp.

Reply
Thread Tools

Deleting lines matching regexp.

 
 
Nikita Synytskyy
Guest
Posts: n/a
 
      02-02-2004
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.

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      02-02-2004
In article <bvm732$tj930$(E-Mail Removed)-berlin.de>,
Nikita Synytskyy <(E-Mail Removed)> 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
articular regexp. I know how to do a substitution:

erl -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.
 
Reply With Quote
 
 
 
 
J Krugman
Guest
Posts: n/a
 
      02-02-2004
In <bvm7u8$83m$(E-Mail Removed)> http://www.velocityreviews.com/forums/(E-Mail Removed)-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

 
Reply With Quote
 
Nikita Synytskyy
Guest
Posts: n/a
 
      02-02-2004
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.

 
Reply With Quote
 
gnari
Guest
Posts: n/a
 
      02-02-2004
"Nikita Synytskyy" <(E-Mail Removed)> wrote in message
news:bvm732$tj930$(E-Mail Removed)-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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting data from the file without deleting the file first crea C++ 2 12-28-2012 11:50 PM
Regex: deleting non-matching words pete Perl Misc 3 08-23-2010 12:43 AM
Help with Pattern matching. Matching multiple lines from while reading from a file. Bobby Chamness Perl Misc 2 05-03-2007 06:02 PM
Deleting a File from Hardrive and Deleting a SubKey in Registry Harry Barker C++ 2 04-19-2006 09:34 AM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM



Advertisments