S&R question

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

S&R question

Eric Leenman
Hi,

How do you search and delete lines that contain contain AAA first and
secondly BBB in the line as below?

AAA vwxyz(10) BBB blabla
AAA def(12) BBB talktalktalk

Rgds,

Eric


Disclaimer; The information contained in this communication is confidential
and may be legally privileged. It is intended solely for the use of the individual
or entity to whom it is addressed and others authorized to recieve it.
Use of this information, in whatever way, by others is strictly prohibited
and may be unlawful. GREEFA does not accept legal responsibility
for this e-mail message due to the insecure nature of internet
communications.  
Reply | Threaded
Open this post in threaded view
|

Re: S&R question

Pete Johns
On Fri, 2005-09-30 at 09:57:18 +0200, Eric Leenman sent:
>Hi,
>
Hello, there!


>How do you search and delete lines that contain contain AAA
>first and secondly BBB in the line as below?
>
This is the way that I would do it:

  :g/^AAA.*BBB.*$/d

See:

  :help :global

Hope this helps;


--paj

--
Pete Johns                                   <http://johnsy.com/>
Tel/Fax numbers and IM information:  <http://johnsy.com/contact/>
Pete's iPod R.I.P. <http://johnsy.com/blog/?entry=20050928152608>
Reply | Threaded
Open this post in threaded view
|

Re: S&R question

Marcin Komorowski
In reply to this post by Eric Leenman
This is an excellent question and I would love to know if it can be done in
pure vim.

I tend to do it by piping the content through grep, like this:
    :%!grep -v 'AAA.*BBB'

Cheers,
Marcin

----- Original Message -----
From: "Eric Leenman" <[hidden email]>
To: "vim" <[hidden email]>
Sent: Friday, September 30, 2005 3:57 AM
Subject: S&R question


> Hi,
>
> How do you search and delete lines that contain contain AAA first and
> secondly BBB in the line as below?
>
> AAA vwxyz(10) BBB blabla
> AAA def(12) BBB talktalktalk
>
> Rgds,
>
> Eric
>
>
> Disclaimer; The information contained in this communication is
> confidential
> and may be legally privileged. It is intended solely for the use of the
> individual
> or entity to whom it is addressed and others authorized to recieve it.
> Use of this information, in whatever way, by others is strictly prohibited
> and may be unlawful. GREEFA does not accept legal responsibility
> for this e-mail message due to the insecure nature of internet
> communications.
>


Reply | Threaded
Open this post in threaded view
|

Re: S&R question

Tim Chase-2
In reply to this post by Pete Johns
>>How do you search and delete lines that contain contain AAA
>>first and secondly BBB in the line as below?
>
> This is the way that I would do it:
>
>   :g/^AAA.*BBB.*$/d

This will find & delete lines where the "AAA" is the first thing
on the line.  If you just want "AAA" preceeding "BBB" anywhere on
the line (even if stuff preceeds the "AAA"), you can do

        :g/AAA.*BBB/d

Additionally, since you're deleting the whole line, the ".*$" is
a bit superflouous.  If the "AAA.*BBB" matches (whether anchored
at the beginning of the line with the "^" or not), the line, and
its remainder (the stuff after the "BBB") will be deleted.

And one other thing that might be helpful is if you want the
*word* "AAA" and "BBB", but not if they exist in other words, you
can wrap them in \<...\> such as

        :g/\<AAA\>.*\<BBB\>/d

Hope this helps,

-tim





Reply | Threaded
Open this post in threaded view
|

Re: S&R question

Marcin Komorowski
In reply to this post by Pete Johns
And there it is..... ;)

Pete, your pattern only works for lines that start with AAA, also you do not
need '.*$' since the :g command by default operates on whole lines that
match the search pattern, so this is the simplest pattern for AAA starting
anywhere in the line:
    :g/AAA.*BBB/d

Cheers,
Marcin

----- Original Message -----
From: "Pete Johns" <[hidden email]>
To: "vim" <[hidden email]>
Sent: Friday, September 30, 2005 4:04 AM
Subject: Re: S&R question


> On Fri, 2005-09-30 at 09:57:18 +0200, Eric Leenman sent:
>>Hi,
>>
> Hello, there!
>
>
>>How do you search and delete lines that contain contain AAA
>>first and secondly BBB in the line as below?
>>
> This is the way that I would do it:
>
>  :g/^AAA.*BBB.*$/d
>
> See:
>
>  :help :global
>
> Hope this helps;
>
>
> --paj
>
> --
> Pete Johns                                   <http://johnsy.com/>
> Tel/Fax numbers and IM information:  <http://johnsy.com/contact/>
> Pete's iPod R.I.P. <http://johnsy.com/blog/?entry=20050928152608>
>


Reply | Threaded
Open this post in threaded view
|

Re: S&R question

Charles E Campbell Jr
In reply to this post by Eric Leenman
Eric Leenman wrote:

>How do you search and delete lines that contain contain AAA first and
>secondly BBB in the line as below?
>
>AAA vwxyz(10) BBB blabla
>AAA def(12) BBB talktalktalk
>  
>

:g/AAA.*BBB/d

Regards,
Chip Campbell

Reply | Threaded
Open this post in threaded view
|

Re: S&R question

Pete Johns
In reply to this post by Marcin Komorowski
On Fri, 2005-09-30 at 08:54:46 -0400, Marcin Komorowski sent:
>Pete, your pattern only works for lines that start with AAA,
>also you do not need '.*$' since the :g command by default
>operates on whole lines that match the search pattern, so this
>is the simplest pattern for AAA starting anywhere in the line:
>:g/AAA.*BBB/d
>
Indeed, you are correct, Marcin.

I included the caret at the beginning of the pattern as in your
inxample all of the lines began with AAA.

Like you say, the ".*$" is superflous, but I tend to include such
things for completeness. I should have mentioned this in my
original response.

Tim Chase raises some good points in his response, too.

With Vim there is usually more than one solution to a given
problem, I'm glad that you have found at least one!


Best;


--paj

--
Pete Johns                                   <http://johnsy.com/>
Tel/Fax numbers and IM information:  <http://johnsy.com/contact/>
Pete's iPod R.I.P. <http://johnsy.com/blog/?entry=20050928152608>