Tuesday, November 30, 2010

Re: filter txt file based on start and ending marks

Hey Tim

Is the "start" and "end" something you have to add yourself? Is it
possible to do this: Let's say that once I find the pattern
"interesting", I'd like to select the line that contains
"interesting", or maybe include the line above and the line below that
line that contains "interesting", for a total of 3 lines, or perhaps
the entire paragraph that contains "interesting". Then, "start" is
automatically put at the top of the paragraph/line(s), and "end" is
put at the bottom of the paragraph/line(s). Now, I can do pattern
matches or substitutions only on ranges between "start" and "end" that
surround "interesting".

Thanks for any info


On Nov 28, 1:56 pm, Tim Chase <v...@tim.thechases.com> wrote:
> On 11/28/2010 09:07 AM, Jeri Raye wrote:
>
> > I was wondering to search for certain patterns in the log file and
> > provide a start and end mark for the interresting stuff.
> > And then delete all that isn't in the selected sections.
> > Is that possible?
>
> While it's a little hard to determine the particulars from your
> description and pseudo-examples, the general process I use to do
> something similar looks like
>
>    :g/interesting/sil! ?start?,/end/s/^/XXX/
>    :v/^XXX/d
>    :%s/^XXX
>
> This searches for every line matching the pattern "interesting",
> then searches backwards for the pattern "start" as the beginning
> of the range, and forward to the pattern "end".  With each of
> those start...end ranges, it tacks on a unique prefix (in this
> case, "XXX")  The "sil!" is just to prevent it from reporting
> action on every matching line because it annoys me :)
>
> Now that you've found and marked the interesting stuff, you can
> delete the rest (the "v/^XXX/d").
>
> Finally, remove the "XXX" marker leaving you with just what you want.
>
> Each of those patterns can be an arbitrary regexp, so you can use
> multiple conditions:
>
>    :g/foo\|bar\|baz/sil! ...
>
> Alternatively, I'll use ">" as my command instead of "s/^/XXX" to
> indent the lines of interest (best done with 'noet' and 'sw'='ts'
> so you get one tab of indent), delete the non-indented lines, and
> then un-indent the indented lines:
>
>    :g/interesting/sil! ?start?,/end/>
>    :v/^\t/d
>    :%<
>
> Hope this gives you a general pattern you can use.
>
> -tim

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment