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

25 views
Skip to first unread message

ping

unread,
Jun 14, 2012, 4:36:22 PM6/14/12
to vim-use Mailingliste
guys:
I run into an issue about :global right now.
with :g, it looks I can grep all lines with a "multi-line pattern" , but only the 1st matched line will be displayed.

this is how ":help :g" says:
 The global commands work by first scanning through the [range] lines and
 marking each line where a match occurs (for a multi-line pattern, only the
 start of the match matters).

is there anyway to make :global display all lines instead of only 1st line?

















===============usage cases example, ignore if it's too long for you=======================

see I want to extract certain line from following text, and only display those lines for me.
usually I use :global to do that job and it works fine.
but the specialty here is, I want to search "across" lines:
I want to get ONLY lines containing either of these:

   "classifier-group"
   OR
   xxx packets, xxx bytes

where xxx can be any NUMBERS.

so I use this after visual selection:

:'<,'>g/classifier-group\_.\{-}\d\d* packets, \d\d* bytes

but I only get these:
:'<,'>g/classifier-group\_.\{-}\d\d* packets, \d\d* bytes

    classifier-group dhcp entry 1
    classifier-group jnpr-VIDEO-TRAFFIC entry 1
    ...<snipped>...
    classifier-group VZ-SMTP-ALLOW-OUT entry 8
    classifier-group SMTP-DENY-OUT entry 1
    classifier-group DOS-FILTER entry 1
Press ENTER or type command to continue

I hit enter, actually found those lines I wanted are correctly highlighted.
how to extract all lines I wanted?

    classifier-group dhcp entry 1
      313 packets, 118332 bytes
    classifier-group jnpr-VIDEO-TRAFFIC entry 1
      0 packets, 0 bytes
    ......

======my file content======================================================================

GigabitEthernet13/0/0.1610022 line protocol VlanSub is up, ip is up
  Network Protocols: IP
  Unnumbered Interface on loopback100
  ( IP address  96.254.154.1 )
  Operational MTU = 1500  Administrative MTU = 0
  Operational speed = 1000000000  Administrative speed = 0
  Discontinuity Time = 3854458745
  Router advertisement = disabled
  Proxy Arp = enabled
  ARP spoof checking = enabled
  Network Address Translation is disabled
  TCP MSS Adjustment = disabled
  SA-validation Enabled
  Administrative debounce-time = disabled
  Operational debounce-time    = disabled
  Access routing = disabled
  Multipath mode = hashed
  Auto Configure = disabled
  Auto Detect = disabled
  Re-Authenticate Auto Detect = disabled
  Append virtual-router name with DSI = disabled
  Inactivity Timer = disabled
  Use Framed Routes = disabled
  Warm-restart initial-sequence-preference: Operational = 0 Administrative = 0
          
  In Received Packets 70424, Bytes 8631290
    Unicast Packets 70424, Bytes 8631290
    Multicast Packets 0, Bytes 0
  In Policed Packets 151, Bytes 143865
  In Error Packets 0
  In Invalid Source Address Packets 0
  In Discarded Packets 151
  Out Forwarded Packets 17706812, Bytes 23798954932
    Unicast Packets 17706812, Bytes 23798954932
    Multicast Routed Packets 0, Bytes 0
  Out Scheduler Dropped Packets 0, Bytes 0
  Out Policed Packets 0, Bytes 0
  Out Discarded Packets 0

  IP policy input jnpr-VIS-PBR
    classifier-group dhcp entry 1
      313 packets, 118332 bytes
      rate-limit-profile dhcplimit
        committed rate: 1280 bps, committed burst: 8192 bytes
        excess burst: 0 bytes
        committed: 313 packets, 118332 bytes, action: transmit
        conformed: 0 packets, 0 bytes, action: drop
        exceeded:  0 packets, 0 bytes, action: drop
    classifier-group jnpr-VIDEO-TRAFFIC entry 1
      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
      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 3
      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 4
      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 5
      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 6
      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 7
      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 8
      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 9
      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 10
      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 11
      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 12
      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 13
      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 14
      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 15
      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 16
      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 17
      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 18
      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 19
      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 20
      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 21
      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 22
      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 23
      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 24
      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 25
      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 26
      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 27
      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 28
      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 29
      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 30
      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 31
      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 32
      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 33
      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 34
      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 35
      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 36
      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 37
      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 38
      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 39
      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 40
      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 41
      58658 packets, 8889186 bytes
      rate-limit-profile video-upstream
        committed rate: 218000 bps, committed burst: 32000 bytes
        excess burst: 0 bytes
        committed: 58572 packets, 8760125 bytes, action: transmit
        conformed: 0 packets, 0 bytes, action: drop
        exceeded:  86 packets, 129061 bytes, action: drop
    classifier-group VZ-SMTP-ALLOW-IN entry 1
      0 packets, 0 bytes
      forward
    classifier-group VZ-SMTP-ALLOW-IN entry 2
      0 packets, 0 bytes
      forward
    classifier-group VZ-SMTP-ALLOW-IN entry 3
      0 packets, 0 bytes
      forward
    classifier-group VZ-SMTP-ALLOW-IN entry 4
      0 packets, 0 bytes
      forward
    classifier-group VZ-SMTP-ALLOW-IN entry 5
      0 packets, 0 bytes
      forward
    classifier-group VZ-SMTP-ALLOW-IN entry 6
      0 packets, 0 bytes
      forward
    classifier-group VZ-SMTP-ALLOW-IN entry 7
      0 packets, 0 bytes
      forward
    classifier-group VZ-SMTP-ALLOW-IN entry 8
      0 packets, 0 bytes
      forward
    classifier-group SMTP-DENY entry 1
      0 packets, 0 bytes
      filter
  IP policy output 111
    classifier-group VZ-SMTP-ALLOW-OUT entry 1
      0 packets, 0 bytes
      forward
    classifier-group VZ-SMTP-ALLOW-OUT entry 2
      0 packets, 0 bytes
      forward
    classifier-group VZ-SMTP-ALLOW-OUT entry 3
      0 packets, 0 bytes
      forward
    classifier-group VZ-SMTP-ALLOW-OUT entry 4
      0 packets, 0 bytes
      forward
    classifier-group VZ-SMTP-ALLOW-OUT entry 5
      0 packets, 0 bytes
      forward
    classifier-group VZ-SMTP-ALLOW-OUT entry 6
      0 packets, 0 bytes
      forward
    classifier-group VZ-SMTP-ALLOW-OUT entry 7
      0 packets, 0 bytes
      forward
    classifier-group VZ-SMTP-ALLOW-OUT entry 8
      0 packets, 0 bytes
      forward
    classifier-group SMTP-DENY-OUT entry 1
      0 packets, 0 bytes
      filter
    classifier-group DOS-FILTER entry 1
      0 packets, 0 bytes
      filter
          
  Attached QoS profile: DATA15M:VIDEO:VLAN @ vlan GigabitEthernet13/0/0.1610022
  queue 0: traffic class best-effort, bound to vlan
GigabitEthernet13/0/0.1610022
    Queue length 0 bytes
    Forwarded packets 925080, bytes 1272991460
    Dropped committed packets 0, bytes 0
    Dropped conformed packets 0, bytes 0
    Dropped exceeded packets 0, bytes 0
  queue 1: traffic class RT-VIDEO, bound to vlan GigabitEthernet13/0/0.1610022
    Queue length 0 bytes
    Forwarded packets 16746496, bytes 22942010364
    Dropped committed packets 0, bytes 0
    Dropped conformed packets 0, bytes 0
    Dropped exceeded packets 0, bytes 0

Christian Brabandt

unread,
Jun 14, 2012, 4:50:12 PM6/14/12
to vim-use Mailingliste
Hi ping!

On Do, 14 Jun 2012, ping wrote:

> see I want to extract certain line from following text, and only
> display those lines for me.
> usually I use :global to do that job and it works fine.
> but the specialty here is, I want to search "across" lines:
> I want to get ONLY lines containing either of these:
>
> "classifier-group"
> OR
> xxx packets, xxx bytes
>
> where xxx can be any NUMBERS.
>
> so I use this after visual selection:
>
> :'<,'>g/classifier-group\_.\{-}\d\d* packets, \d\d* bytes
>
> but I only get these:
> :'<,'>g/classifier-group\_.\{-}\d\d* packets, \d\d* bytes


I am not sure I understand, which of 2 different problems you are trying
to solve. If you wish to print lines containing either
"classifier-group" or "xxx packets, xxx bytes" use something like this
command:
:g/classifier-group\|\d\d* packets, \d\d* bytes


Or if you want to print lines from the "classifier-group" line until the
next matching line "xxx" packets, xxx bytes", use this command:

:g/classifier-group/.,/\d\d* packets, \d\d* bytes/p

(Which boils down to on each line matching classifier-group" print from
there until the next line, matching '\d\d packets, \d\d bytes' using the
:p command.)


regards,
Christian
--
Der Mensch ist nicht geboren, frei zu sein,
und f�r den Edlen ist kein sch�ner Gl�ck,
als einem F�rsten, den er ehrt, zu dienen.
-- Johann Wolfgang von Goethe (Torquato Tasso)

ping

unread,
Jun 14, 2012, 5:17:55 PM6/14/12
to vim-use Mailingliste
hi Chris,
thanks for quick response.
yes I didn't say it clear.
actually the latter as you mentioned is exactly what I want:

"
Or if you want to print lines from the "classifier-group" line until the 
next matching line "xxx" packets, xxx bytes", use this command:

:g/classifier-group/.,/\d\d* packets, \d\d* bytes/p

(Which boils down to on each line matching classifier-group" print from 
there until the next line, matching '\d\d packets, \d\d bytes' using the 
:p command.)
"
to be more precise , I want to extract only those adjacent lines like following,

    classifier-group jnpr-VIDEO-TRAFFIC entry 2
      0 packets, 0 bytes


but only if the packets, bytes are NON-zero. so I modified your command to:
:g/classifier-group/.,/[1-9]\d* packets, [1-9]\d* bytes/p

Looks I don't get anything after that, do I miss anything here?


thanks!

regards
ping

Ben Fritz

unread,
Jun 14, 2012, 10:39:03 PM6/14/12
to vim...@googlegroups.com
On Thursday, June 14, 2012 4:17:55 PM UTC-5, ping wrote:
>
> to be more precise , I want to extract only those adjacent lines
> like following,
>
>
>
> <font size="+1"><font face="monospace">    classifier-group
> jnpr-VIDEO-TRAFFIC entry 2
>
>       0 packets, 0 bytes</font></font>
>

Thank you for finally being precise.

Now we can answer your question.

First, is there any way you can stop using the obnoxiously formatted HTML
emails? The list convention is plaintext, and the large font size is
especially jarring.

Anyway...you learned that a :g command only runs a command on the first line
of a match, if you specify a multi-line pattern. But, you want to specify
which lines to act on based on a multi-line match; i.e., a line with
"classifier-group" followed by a line with "X packets, Y bytes" where X and
Y are both non-zero numbers. So you will need to use a multi-line regex, and
also the trick shown by others in this thread, to specify a range for the
command which :g runs on each matching first line, so that you can print
multiple lines starting at each first line of your multi-line regex.

I think, this should do what you want:

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

Note carefully what this does: it matches a multi-line pattern of exactly
two lines with the characteristics you describe, and runs the "print"
command on the first line that matches, plus the next line (thus, both
matched lines).

ping

unread,
Jun 14, 2012, 11:38:42 PM6/14/12
to vim...@googlegroups.com, Ben Fritz
hi Ben:
thanks for the answer and explanation.

sorry about the html email - I didn't notice that and I think I just set
it up to text emails only. Please do let me know if there is still an issue.

yes the answer you provided is exactly what I'm looking for, great to
learn and appreciate the explanation. Now I also understand Christian'
suggestion better :)

this
[range-start via :g],[range-end via line offset]p
method is cool...

now I'm thinking one step further :)
is there a more "scalable" method?
in my given example I only need "2 continuous lines", what if I actually
don't know (or don't want to count) how many lines it will match and I
simply just need to print them all (from the 1st to the end of matched
lines)?

///this doesn't work
:g#classifier-group\>.*\n\s*[1-9]\d* packets, [1-9]\d* bytes#.,line('$')p

E492: Not an editor command: ,line('$')p



here per the :help line() it looks line('$') might be the "last line"
that I need here as a range:

line({expr}) The result is a Number, which is the line number of the file
position given with {expr}. The accepted positions are:
. the cursor position
$ the last line in the current buffer
'x position of mark x (if the mark is not
set, 0 is
returned)
w0 first line visible in current window
w$ last line visible in current window
v In Visual mode: the start of the Visual
area (the
cursor is the end). When not in Visual mode
returns the cursor position. Differs
from '< in
that it's updated right away.
Note that a mark in another file can be used. The
line number
then applies to another buffer.
To get the column number use col(). To get both use
getpos().
Examples: >
line(".") line number of the cursor
line("'t") line number of mark t
line("'" . marker) line number of mark
marker

Tony Mechelynck

unread,
Jun 15, 2012, 12:13:59 AM6/15/12
to vim...@googlegroups.com, ping, Ben Fritz
On 15/06/12 05:38, ping wrote:
> hi Ben:
> thanks for the answer and explanation.
>
> sorry about the html email - I didn't notice that and I think I just set
> it up to text emails only. Please do let me know if there is still an
> issue.
>
> yes the answer you provided is exactly what I'm looking for, great to
> learn and appreciate the explanation. Now I also understand Christian'
> suggestion better :)
>
> this
> [range-start via :g],[range-end via line offset]p
> method is cool...
>
> now I'm thinking one step further :)
> is there a more "scalable" method?
> in my given example I only need "2 continuous lines", what if I actually
> don't know (or don't want to count) how many lines it will match and I
> simply just need to print them all (from the 1st to the end of matched
> lines)?
[...]

In your range, the range-end can be a search command, see :help [range]

For instance, to display any lines which are between <table> and
</table> included:

:%g/<table>/.,/<\/table>/p

This assumes that you are not using embedded tables, which are a legal
but rare construct.

>
> ///this doesn't work
> :g#classifier-group\>.*\n\s*[1-9]\d* packets, [1-9]\d* bytes#.,line('$')p
>
> E492: Not an editor command: ,line('$')p

Instead, to print from the line matched by :g to the end of the file,
you would need g/whatever/.,$p though if there are several matches you
would get repeated (overlapping) printouts.

At that point, Vim expects an ex-command (which can be preceded by a
range). line( is not an ex-command but the start of an arithmetic
expression. You might include it in the argument of the :eval command
(q.v.) but in this case it is not necessary: In a range, the "last line"
is just $ so e.g. # and 1,$ are equivalent as a range for "the whole file".

If there is only one match, you don't need :g -- for instance, to
display the body of an HTML page (but not the <head> and not the
trailing </html> if it is on a line by itself) you could use

:1
:/<body>\c/,/<\/body>\c/p

Replace p by hardcopy to type it out to the printer instead of the display.


Best regards,
Tony.
--
"If once a man indulges himself in murder, very soon he comes to think
little of robbing; and from robbing he next comes to drinking and
Sabbath-breaking, and from that to incivility and procrastination."
-- Thomas De Quincey (1785 - 1859)

ping

unread,
Jun 15, 2012, 12:15:10 AM6/15/12
to vim...@googlegroups.com, Ben Fritz
still I'm thinking the exact syntax of this is kind of hard to
understand...for my uneducated eyes at least...

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

//this means we are "extracting" the matched lines only, means what ever
we'll do next based on this, we'll ignore those un-matched lines, it
seems not the case...
:g#classifier-group\>.*\n\s*[1-9]\d* packets, [1-9]\d* bytes#

//this is very tricky part for me
.,+1p

"." is "current line", for me this clearly should mean the "current
matched line", which is actually 2 real lines here since we are running
the multi-line regex.
but obviously in this context, the "." was used to describe "only the
1st line out of all matched lines", right?

and that "+1":
the "1st matched line" plus "1 more" line, but shouldn't that offset
also be used from within the pool of matched lines? or regardless of
match or not?
my test shows if you put +2 it will print non-matched lines too:

:g/classifier-group.*\n.*[1-9]\d* packets, [1-9]\d* bytes/.,+2p
41 classifier-group dhcp entry 1
42 313 packets, 118332 bytes
43 rate-limit-profile dhcplimit <--non-matched line
369 classifier-group jnpr-VIDEO-TRAFFIC entry 41
370 58658 packets, 8889186 bytes
371 rate-limit-profile video-upstream <--non-matched line

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


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 following patterns will be ignored)
classifier-group dhcp entry 1
rate-limit-profile dhcplimit
)


regards
ping



ok now the tricky part:

Ben Fritz

unread,
Jun 15, 2012, 12:22:41 AM6/15/12
to vim...@googlegroups.com, Ben Fritz
On Thursday, June 14, 2012 10:38:42 PM UTC-5, ping wrote:
> hi Ben:
> thanks for the answer and explanation.
>
> sorry about the html email - I didn't notice that and I think I just set
> it up to text emails only. Please do let me know if there is still an issue.
>
> yes the answer you provided is exactly what I'm looking for, great to
> learn and appreciate the explanation. Now I also understand Christian'
> suggestion better :)
>
> this
> [range-start via :g],[range-end via line offset]p
> method is cool...
>
> now I'm thinking one step further :)
> is there a more "scalable" method?
> in my given example I only need "2 continuous lines", what if I actually
> don't know (or don't want to count) how many lines it will match and I
> simply just need to print them all (from the 1st to the end of matched
> lines)?
>
> ///this doesn't work
> :g#classifier-group\>.*\n\s*[1-9]\d* packets, [1-9]\d* bytes#.,line('$')p
>
> E492: Not an editor command: ,line('$')p
>
>

line('$') won't work for 2 reasons:

1. it's an expression, expressions aren't allowed as commands (but see the :execute command)
2. it refers to the last line in the buffer, not the last line matched by a regex

The key to this version of the problem, is that a range CAN contain a regex.

See http://vim.wikia.com/wiki/Ranges and :help cmdline-ranges

Christian took a stab at this problem with his suggestion:

:g/classifier-group/.,/\d\d* packets, \d\d* bytes/p

This command bears examination.

g/classifier-group/ = run the following command on every line matching "classifier-group"
.,/\d\d* packets, \d\d* bytes/ = range for the command, selecting everything from the current line being processed by the :g command, to the first occurrence of a "packets, bytes" line after that
p = print the lines selected by the range

Unfortunately the range given to the :p command also will run right over a new classifier-group line, printing all lines in between. You'll need to explicitly exclude such lines with a negative look-ahead (see :help /\@!). This regex will become very complicated very quickly! You are rapidly getting into an area where it would be simpler to write a quick Vim script that runs a while loop over all lines in the file, checks line content with getline(), and outputs accordingly.

Ben Fritz

unread,
Jun 15, 2012, 12:34:08 AM6/15/12
to vim...@googlegroups.com, Ben Fritz
On Thursday, June 14, 2012 11:15:10 PM UTC-5, ping wrote:
> still I'm thinking the exact syntax of this is kind of hard to
> understand...for my uneducated eyes at least...
>
> :g#classifier-group\>.*\n\s*[1-9]\d* packets, [1-9]\d* bytes#.,+1p
>
> //this means we are "extracting" the matched lines only, means what ever
> we'll do next based on this, we'll ignore those un-matched lines, it
> seems not the case...
> :g#classifier-group\>.*\n\s*[1-9]\d* packets, [1-9]\d* bytes#
>
> //this is very tricky part for me
> .,+1p
>
> "." is "current line", for me this clearly should mean the "current
> matched line", which is actually 2 real lines here since we are running
> the multi-line regex.
> but obviously in this context, the "." was used to describe "only the
> 1st line out of all matched lines", right?
>

I think you may be confused about what a multi-line match does. The :g
command acts on the lines where a pattern matches. A pattern matches at a
single position: the position of the start of the match. The :g command
therefore only acts on the first line of a multi-line regex: the line
containing the position of the start of the match.

> and that "+1":
> the "1st matched line" plus "1 more" line, but shouldn't that offset
> also be used from within the pool of matched lines? or regardless of
> match or not?

No, it's a line offset. You're saying "print from the current line to one
below the current line", it has no concept of the matches to the :g command,
it fact it doesn't even know it's running in a :g command.

> my test shows if you put +2 it will print non-matched lines too:
>

Yes, because there is only 1 matched line, as discussed above, and this is a
range of line numbers, not a range within the matched lines.

> :g/classifier-group.*\n.*[1-9]\d* packets, [1-9]\d* bytes/.,+2p
> 41 classifier-group dhcp entry 1
> 42 313 packets, 118332 bytes
> 43 rate-limit-profile dhcplimit <--non-matched line
> 369 classifier-group jnpr-VIDEO-TRAFFIC entry 41
> 370 58658 packets, 8889186 bytes
> 371 rate-limit-profile video-upstream <--non-matched line
>
> 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.

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

Tony Mechelynck

unread,
Jun 15, 2012, 12:46:49 AM6/15/12
to vim...@googlegroups.com, ping, Ben Fritz
On 15/06/12 06:15, ping wrote:
> still I'm thinking the exact syntax of this is kind of hard to
> understand...for my uneducated eyes at least...
>
> :g#classifier-group\>.*\n\s*[1-9]\d* packets, [1-9]\d* bytes#.,+1p
>
> //this means we are "extracting" the matched lines only, means what ever
> we'll do next based on this, we'll ignore those un-matched lines, it
> seems not the case...
> :g#classifier-group\>.*\n\s*[1-9]\d* packets, [1-9]\d* bytes#

the above means "every time there is a match for the pattern between the
#. If (as is more usual) it were shown between / it would be

/classifier-group\>.*\n\s*[1-9]\d* packets, [1-9]\d* bytes/

which would mean matching the following in sequence:

"classifier-group"
an end of word
any number of characters (as many as possible) on the same line
one line break
any number of spaces and/or tabs (as many as possible)
a character between 1 and 9 inclusive
any number of digits (as many as possible)
" packets, "
a character between 1 and 9 inclusive
any number of digits (as many as possible)
" bytes "

To replace "one line break" by "any number of lines, zero or more, as
few as possible" replace \n by \_$\_.\{-}\_^ which means

an end-of-line, also if not at the end of the pattern
any number of anything, including line breaks, as few as possible
a start-of-line, also if not at the start of the pattern

>
> //this is very tricky part for me
> .,+1p
>
> "." is "current line", for me this clearly should mean the "current
> matched line", which is actually 2 real lines here since we are running
> the multi-line regex.
> but obviously in this context, the "." was used to describe "only the
> 1st line out of all matched lines", right?

It means "every matched line". Since you got a multi-line pattern, it
means (IIUC) "the first line of every match".

>
> and that "+1":
> the "1st matched line" plus "1 more" line, but shouldn't that offset
> also be used from within the pool of matched lines? or regardless of
> match or not?

That offset is relative to the part before the comma, i.e. it means "the
line after the first line of every match".

> my test shows if you put +2 it will print non-matched lines too:

If you put +2 it will always print 3 lines starting at the first line of
every match.

>
> :g/classifier-group.*\n.*[1-9]\d* packets, [1-9]\d* bytes/.,+2p
> 41 classifier-group dhcp entry 1
> 42 313 packets, 118332 bytes
> 43 rate-limit-profile dhcplimit <--non-matched line
> 369 classifier-group jnpr-VIDEO-TRAFFIC entry 41
> 370 58658 packets, 8889186 bytes
> 371 rate-limit-profile video-upstream <--non-matched line
>
> 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
>
>
> 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?

that's harder: a :p command can print any number of *consecutive* lines.
But the :global command regards a | as part of its argument, so you
could have

:g/pattern/p|.+2p

to always print the (first) matching line and the second line after it.

See :help :Bar

Similarly, to print every line containing /pat1/ and (every time) the
first line containing /pat2/ after it, you could (IIUC) use

:g/pat1/p|/pat2/p

>
> (so following patterns will be ignored)
> classifier-group dhcp entry 1
> rate-limit-profile dhcplimit
> )
>
>
> regards
> ping

Oh, and BTW the preferred reply style in these groups is to put the
reply text below what you're replying to, and snip away irrelevant text
from older messages.


Best regards,
Tony.
--
The human animal differs from the lesser primates in his passion for
lists of "Ten Best".
-- H. Allen Smith

ping

unread,
Jun 15, 2012, 10:53:45 AM6/15/12
to vim...@googlegroups.com, Ben Fritz


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

Ben Fritz

unread,
Jun 15, 2012, 11:07:52 AM6/15/12
to vim...@googlegroups.com
On Friday, June 15, 2012 9:53:45 AM UTC-5, ping wrote:
>
> [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)?
>

You might be able to do it using a search pattern as the range to a print command.

But for something this complex I'd probably just write a while loop in a *.vim file, like:

let lineNum = 1
let savedLine = ""
while lineNum < line('$')
let lineTxt = getline(lineNum)
if lineTxt =~ 'some pattern of your choosing'
let savedLine = lineTxt
elseif lineTxt =~ 'some other pattern'
echo savedLine
echo lineTxt
endif
let lineNum += 1
endwhile

Then use :source whatever.vim on the file you want to parse.

If you use this often you can place it in a function and create a command and put it in your .vimrc somewhere:

function! MyPrettyParser()
...while loop from above...
endfunction

command! ParseSomething call MyPrettyParser()

Reply all
Reply to author
Forward
0 new messages