delete lines NOT containing pattern.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

delete lines NOT containing pattern.

Jeremy Eliosoff
Hi there,

I'm trying to delete all lines in my file that DON'T contain the word
"parameter".  I've tried this:

:%s/\(^.*parameter.*$\)\@!//gc

but that deletes "parameter"-containing lines as well as all others.
This confuses me because

:%s/\(^.*parameter.*$\)//gc

deletes only "parameter"-containing lines.  I assumed that adding the
\@! simply negates/inverts the pattern, matching all strings EXCEPT
those that match the preceding atom.  Please help!

Thanks,

Jeremy.
Reply | Threaded
Open this post in threaded view
|

Re: delete lines NOT containing pattern.

Tim Chase-2
> I'm trying to delete all lines in my file that DON'T contain
> the word "parameter".  I've tried this:


:v/parameter/d

-tim


Reply | Threaded
Open this post in threaded view
|

RE: delete lines NOT containing pattern.

Keith W. Roberts
In reply to this post by Jeremy Eliosoff
 

> -----Original Message-----
> From: Jeremy Eliosoff [mailto:[hidden email]]
> Sent: Friday, September 09, 2005 2:36 PM
> To: [hidden email]
> Subject: delete lines NOT containing pattern.
>
> Hi there,
>
> I'm trying to delete all lines in my file that DON'T contain the word
> "parameter".  I've tried this:
>
> :%s/\(^.*parameter.*$\)\@!//gc
>
> but that deletes "parameter"-containing lines as well as all others.
> This confuses me because
>
> :%s/\(^.*parameter.*$\)//gc
>
> deletes only "parameter"-containing lines.  I assumed that adding the
> \@! simply negates/inverts the pattern, matching all strings EXCEPT
> those that match the preceding atom.  Please help!
>

Tim already gave you the best way, but let's see if I can explain why yours
failed.

The problem is that, as stated, your pattern matches *every* line, because
the \@! is a zero-length match, and there is nothing *outside* the
"exclusion" which must match.  So, instead ...

You start with an anchor:
/^

Then you want to *exclude* ANYTHING followed by the string:
/^\(.*parameter\)\@!

Then grab anything left in the line:
/^\(.*parameter\)\@!.*$

This way you're anchored at both ends.  You can leave off the last part if
you're just doing a search, but the idea is that you want to match the whole
line so you can delete it.

One more change before you substitute will work: $ is also a zero-length
atom, so replace it with \n, which causes the eol to also be deleted.
Hence:
:%s/^\(.*parameter\)\@!.*\n//

QED

-Keith


Reply | Threaded
Open this post in threaded view
|

Re: delete lines NOT containing pattern.

Charles E Campbell Jr
In reply to this post by Jeremy Eliosoff
Jeremy Eliosoff wrote:

>I'm trying to delete all lines in my file that DON'T contain the word
>"parameter".  I've tried this:...snip
>  
>

As mentioned by several others, you should use :v/parameter/d   -or-  
:g!/parameter/d.

 However, sometimes one does want to look for lines that don't contain
some string,
or contain some string but not another, etc.  For that purpose, check
out LogiPat,
available at my website:
  http://mysite.verizon.net/astronaut/vim/index.html#VimFuncs (as
"LogiPat").

Regards,
Chip Campbell