Friday, June 15, 2012

Re: vim: problem when searching and displaying certain lines with :global

On 06/15/2012 12:34 AM, Ben Fritz wrote:

>>
>> so the right logic here looks is:
>>
>> :g/.../ find some matched line(S),
>> no matter how many lines got matched, take only 1st line, trash all others
>> use that as the start of the range
>> use another offset (here +1) based on original text (not matched lines),
>> as end of range
>> print
>>
>
> Basically...yes.
>

[ping] thanks for the literal response, I think I learned a lot from you
guys!

>>
>> Then what if I want:
>> the line containing classifier-group
>> followed by a line x packets, y bytes
>> followed by a line rate-limit-profile
>> but I only want 1st& 3rd line under these constraint, since only these
>> are interested lines?
>>
>
> so you'd want to match all 3 lines in your :g command:
>
> :g#classifier-group.*\n.*[1-9]\d* packets, [1-9]\d* bytes.*\n.*rate-limit-profile#
>
> then print just the first line and the 3rd line (but not the second line):
>
> p | +2p
>

[ping] this I tested, yes it works!
still I think ... here we happen to know which line out of the original
text will be the matched lines, so we can just specify the line with a
static number there...what if we don't know (or hard to count)?

say from these texts snips:

classifier-group dhcp entry 1 <----wanted block
313 packets, 118332 bytes
rate-limit-profile dhcplimit
committed rate: 1280 bps, committed burst: 8192 bytes <--wanted
excess burst: 0 bytes
committed: 313 packets, 118332 bytes, action: transmit <-wanted
conformed: 0 packets, 0 bytes, action: drop
exceeded: 0 packets, 0 bytes, action: drop
classifier-group jnpr-VIDEO-TRAFFIC entry 1 <--not wanted block
0 packets, 0 bytes
rate-limit-profile video-upstream
committed rate: 218000 bps, committed burst: 32000 bytes
excess burst: 0 bytes
committed: 0 packets, 0 bytes, action: transmit
conformed: 0 packets, 0 bytes, action: drop
exceeded: 0 packets, 0 bytes, action: drop
classifier-group jnpr-VIDEO-TRAFFIC entry 2 <--not wanted block
0 packets, 0 bytes
rate-limit-profile video-upstream
committed rate: 0 bps, committed burst: 0 bytes
excess burst: 0 bytes
committed: 0 packets, 0 bytes, action: transmit
conformed: 0 packets, 0 bytes, action: drop
exceeded: 0 packets, 0 bytes, action: drop

it is composed by a lot of (around 41 or sometime far more)
"classifier-group" blocks.

In my work I frequently run into a need of a nick&quick way to show my
customer some data of the following lines, but ONLY in specific
classifier-group blocks that with:
1) lines containing "classifier-group" (so I know which category the
counters falls into)
AND
2) a non-zero "xxx bps, committed burst:" line
AND
3) a non-zero "committed: xxx packets, xxx bytes" line
AND
4) if there is no match on 2) and 3), i.e if there is no wanted data,
skip the block

so in this specific example, ideally only lines like in the 1st block
get extracted out:

classifier-group dhcp entry 1
313 packets, 118332 bytes
committed rate: 1280 bps, committed burst: 8192 bytes
committed: 313 packets, 118332 bytes, action: transmit
<no other lines>
<no other blocks>

currently I think a simple method for beginners like me will be a
multiple steps work:

1) extract all single lines using 3 individual patterns
:g#classifier-group\|[1-9]\d* bps, committed burst:\|committed:[1-9]\d*
packets, [1-9]\d* bytes#

I'll get these:

classifier-group dhcp entry 1
313 packets, 118332 bytes
committed rate: 1280 bps, committed burst: 8192 bytes
committed: 313 packets, 118332 bytes, action: transmit
classifier-group jnpr-VIDEO-TRAFFIC entry 1
classifier-group jnpr-VIDEO-TRAFFIC entry 2

2) put it in a buffer (this I am learning from today's Nick's topic of
"redirection of global print to register" :p )
...
I've learned a better way to make it one line from help:
:redir @c | g/a./^@redir END
where ^@ is one character, it's newline and it's inserted with C-V C-J
...

I'll test that.

3) from the new buffer, do a 2nd time work, using our current solution
of muti-line pattern way:

:g#
classifier-group\>.*\n
.*[1-9]\d* bps, committed burst:.*\n
.*committed:[1-9]\d* packets, [1-9]\d* bytes.*
#.,+2p

please advice if there is a better ONE-TIME way and
sorry if I'm too greedy...

--
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