working with a range of lines between two patterns

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

working with a range of lines between two patterns

Mike Blonder-2
Hello.

I need to delete a range of lines that are found between two patterns across a
lot of files.  The patterns are two comments in an html file type.  I have
done a lot of searching, but have been unable to find a syntax example for a
command that can be run with argdo to delete the range of lines in each file.  
There are 22 lines between the two patterns.  The linefeed character used in
the text is ^J.  I am unable to produce a ^J with a ^V key sequence, so am
stuck as I cannot copy the entire 22 lines of text into a substitution or
global command.

Thanks for any help that anyone can offer on this.

Mike


Reply | Threaded
Open this post in threaded view
|

Re: working with a range of lines between two patterns

Yakov Lerner

On Sun, 19 Feb 2006 08:10:30 -0500, [hidden email] said:
> I need to delete a range of lines that are found between two patterns
> across a
> lot of files.  The patterns are two comments in an html file type.  I
> have
> done a lot of searching, but have been unable to find a syntax example
> for a
> command that can be run with argdo to delete the range of lines in each
> file.  

The command is:
    :/pattern1/,/pattern2/d

Yakov
--
 
  [hidden email]

--
http://www.fastmail.fm - The way an email service should be

Reply | Threaded
Open this post in threaded view
|

Re: working with a range of lines between two patterns

Tim Chase-2
> The command is: :/pattern1/,/pattern2/d

Yakov's solution works if you only have one instance of
/pattern1/,/pattern2/.  However, if you have more than one
instance in the file that you want to delete, try

:argdo g/pattern1/.,/pattern2/d

I presume you already are working with 'hidden' set and
using ":wall" to write the files after you've verified them;
or you're writing each file after the deletion so that it
doesn't complain when you try to leave the buffer.

(If you want/need more detailed explanation of those, drop a
line and I can ramble on a bit more :)

-tim





Reply | Threaded
Open this post in threaded view
|

Re: working with a range of lines between two patterns

Gerald Lai-2
On Sun, 19 Feb 2006, Tim Chase wrote:

>> The command is: :/pattern1/,/pattern2/d
>
> Yakov's solution works if you only have one instance of
> /pattern1/,/pattern2/.  However, if you have more than one instance in the
> file that you want to delete, try
>
> :argdo g/pattern1/.,/pattern2/d
[snip]

Tim, I'm just curious about the dot "." for your suggested global
command :) What is its purpose? AFAIK, it wouldn't make a difference if
you had it there or not. I am quite aware of ":h offset" and ":h //;"
but can't seem to find any info on it.
--
Gerald
Reply | Threaded
Open this post in threaded view
|

Re: working with a range of lines between two patterns

Tim Chase-2
>> :argdo g/pattern1/.,/pattern2/d
>
> [snip]
>
> Tim, I'm just curious about the dot "." for your suggested global
> command :) What is its purpose? AFAIK, it wouldn't make a difference if
> you had it there or not. I am quite aware of ":h offset" and ":h //;"
> but can't seem to find any info on it.

I tend to put it in there just to be explicit.  Yes, it can
easily be omitted to the same effect as

        :argdo g/pattern1/,/pattern2/d

I just tend to think in terms of

        :g/pattern/{command}

where, in this case the command is

        .,/pattern2/d

which explicitly states that this is the line I want to
start at.  I will often use a relative command here, such as

        :g/pattern/+,/pattern2/d

where I want to start the command the line after the
/pattern/ match rather than on the /pattern/ match.  Info on
the period target location can be found at

        :help :.

which is where all the other info on relative/absolute
line-number positioning can be found

        :help :range

Looking at it again, I just learned about

        \/

        \?

and

        \&

which I hadn't noticed before when reading this section.  I
suspect that I'd be more prone to use the first two rather
than the third one, but there are all these little things
that I keep learning about Vim.  Cool! :)

-tim





Reply | Threaded
Open this post in threaded view
|

Re: working with a range of lines between two patterns

William Pursell
In reply to this post by Mike Blonder-2
[hidden email] wrote:

> Hello.
>
> I need to delete a range of lines that are found between two patterns across a
> lot of files.  The patterns are two comments in an html file type.  I have
> done a lot of searching, but have been unable to find a syntax example for a
> command that can be run with argdo to delete the range of lines in each file.  
> There are 22 lines between the two patterns.  The linefeed character used in
> the text is ^J.  I am unable to produce a ^J with a ^V key sequence, so am
> stuck as I cannot copy the entire 22 lines of text into a substitution or
> global command.
>
> Thanks for any help that anyone can offer on this.
>
> Mike
>
The following will delete all lines between the
label "START" and the label "END", inclusive.

:/START/,/END/g/.*/d
Reply | Threaded
Open this post in threaded view
|

Re: working with a range of lines between two patterns

Gerald Lai-2
In reply to this post by Tim Chase-2
On Mon, 20 Feb 2006, Tim Chase wrote:

>>> :argdo g/pattern1/.,/pattern2/d
>>
>> [snip]
>>
>> Tim, I'm just curious about the dot "." for your suggested global
>> command :) What is its purpose? AFAIK, it wouldn't make a difference if
>> you had it there or not. I am quite aware of ":h offset" and ":h //;"
>> but can't seem to find any info on it.
>
> I tend to put it in there just to be explicit.  Yes, it can easily be omitted
[snip]

Thanks for the explanation, Tim :)

> :help :range
>
> Looking at it again, I just learned about
>
> \/
>
> \?
>
> and
>
> \&

Now I wonder how we can make good use of these ;)
--
Gerald