filter txt file based on start and ending marks

2 views
Skip to first unread message

Jeri Raye

unread,
Nov 28, 2010, 10:07:41 AM11/28/10
to v...@vim.org
Hi,

When I compile my VHDL design I get a large log file which I want to filter.
Is it possible to do this?
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?

For example, if the text is this:
+--- START OF LOG ---+
[...]
Not interesting stuff.
[...]
AAA ... BBB ... CCC
[...]
Interesting stuff.
[...]
DDD .... EEE
[...]
Not interesting stuff.
[...]
FFF
[...]
Interesting stuff.
[...]
GGG ... HHH
[...]
Not interesting stuff.
[...]
KKK ... LLL ... MMM ... NNN
[...]
Interesting stuff.
[...]
PPP ... QQQ ... RRR
+--- END OF LOG ---+

1) Can you then search for a pattern that starts with AAA and then
followed by BBB and ending with CCC and place a start mark to select
the line
AAA ... BBB ... CCC
(... means any kind of text, but not AAA, BBB or CCC)

2) And then place an end mark at the line
DDD .... EEE

3) Do the same for FFF (place here a start mark) and GGG ... HHH
(place an end mark)
4) Do the same for KKK ... LLL ... MMM ... NNN (place a start mark)
and PPP ... QQQ ... RRR (place an end mark)

5) Now you have a log file with start and ending marks. How to delete
the selection that are not in the start and ending marks?
Or if that's easier, how to copy the sections marked to a new file?

So that the new log file is as:
+--- START OF LOG ---+
AAA ... BBB ... CCC
[...]
Interesting stuff.
[...]
DDD .... EEE
FFF
[...]
Interesting stuff.
[...]
GGG ... HHH
KKK ... LLL ... MMM ... NNN
[...]
Interesting stuff.
[...]
PPP ... QQQ ... RRR
+--- END OF LOG ---+


Rgds,
Jeri

Tim Chase

unread,
Nov 28, 2010, 3:56:44 PM11/28/10
to vim...@googlegroups.com, Jeri Raye, v...@vim.org
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


bboyjkang

unread,
Nov 30, 2010, 5:01:09 PM11/30/10
to vim_use
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
Message has been deleted

Tim Chase

unread,
Nov 30, 2010, 7:17:16 PM11/30/10
to vim...@googlegroups.com, bboyjkang
On 11/30/2010 04:01 PM, bboyjkang wrote:
>> :g/interesting/sil! ?start?,/end/s/^/XXX/
>> :v/^XXX/d
>> :%s/^XXX
>
> 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�.

(Rearranged to trimmed inline-quoting rather than top-posting, as
is preferred on the mailing-list)

Yes, you can give other relative ranges. Note that it breaks down as

:g/pattern/action_to_perform_on_matching_lines

where action happens to be

<range>s/foo/bar/

(prefixed with "sil!" as previously detailed) and where <range>
happens to be "search backwards for pattern A, through searching
forwards to pattern B". For the line above/below version, you
could just change the range from the complex searches to
".-1,.+1" or more tersely, "-,+" as searches are relative to the
current line (the line found by each :g hit) so the "." is
optional, and adding/subtracting lines defaults to 1 if omitted.

:help :range

That would make the command

:g/interesting/sil! .-1,.+1s/^/XXX
or
:g/interesting/sil! -,+s/^/XXX

To adjust the number of lines of context, just change the "-1" to
however many lines you want before, and the "+1" to however many
lines you want after.

Note that you might hit a fence-posting case if the last
"interesting" match, plus whatever forward-context you want (in
this case, one line; in the previous case, searching forward to a
pattern) puts you beyond the end-of-file. So you might want to
hand-verify the final "interesting" match in the file.

-tim

Tim Chase

unread,
Nov 30, 2010, 7:21:06 PM11/30/10
to vim...@googlegroups.com, bboyjkang
On 11/30/2010 04:01 PM, bboyjkang wrote:
> 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).

Oh, I missed the paragraph bits...if they're blank-line separated
paragraphs, you can use the range '{,'} as in

g/interesting/sil! '{,'}s/^/XXX

You can read about those marks at

:help '{
:help '}

-tim

Reply all
Reply to author
Forward
0 new messages