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

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

Reply via email to