Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

regular expression -- cisco "show version" output

160 views
Skip to first unread message

brickwalls19

unread,
Feb 6, 2008, 3:25:44 PM2/6/08
to
I'm working on an Expect script to extract the serial number from a
Cisco "show version" output.

I managed to get close with:

expect -re "(\r\nSystem serial number: )(.*)(\[^\r\n])$" {
send_user "The
number is: $expect_out(2,string)"
}

But it extracted the following:

logging ---
expect: set expect_out(0,string) "\r\nSystem serial number:
FAA0509F0N1\r\nConfiguration register is 0xF\r\n\r\nARB11Switch2#"
expect: set expect_out(1,string) "\r\nSystem serial number: "
expect: set expect_out(2,string) "FAA0509F0N1\r\nConfiguration
register is 0xF\r\n\r\nARB11Switch2"
expect: set expect_out(3,string) "#"

output ---
The number is: FAA0509F0N1
Configuration register is 0xF


Anyone can suggest how I can alter the expression to extract up to the
first "\r\n" after the serial number?
I'm going through the "exploring expect" book, and it looks like my
regular expression does longest match. There are also suggestions to
extract line-by-line and use list manipulation.

I feel I'm close without reworking/adding new lines. Thanks.

Alexandre Ferrieux

unread,
Feb 6, 2008, 3:56:43 PM2/6/08
to
On Feb 6, 9:25 pm, brickwalls19 <brickwall...@gmail.com> wrote:
>
> expect -re "(\r\nSystem serial number: )(.*)(\[^\r\n])$" {
> Anyone can suggest how I can alter the expression to extract up to the
> first "\r\n" after the serial number?

Yes. (.*) is too wide, since it swallows any sequence of characters,
including \r and \n, and it is greedy, meaning "longest match" as you
say. Moreover, I don't understand your single [^\r\n] at the end. I
believe what you want is something like

expect -re "(\r\nSystem serial number: )(\[^\r\n]*)\r\n"

It is preferrable to a non-greedy variant of your expression for
performance considerations.

-Alex

Donal K. Fellows

unread,
Feb 7, 2008, 6:15:21 AM2/7/08
to
brickwalls19 wrote:
> expect -re "(\r\nSystem serial number: )(.*)(\[^\r\n])$" {
> send_user "The number is: $expect_out(2,string)"
[...]

> Anyone can suggest how I can alter the expression to extract up to the
> first "\r\n" after the serial number?

The regular expression/expect statement you are looking for is probably
this:

expect -re {(?n)^System serial number: (\w+)} {
send_user "The number is $expect_out(1,string)"
}

The key to this is three-fold. Firstly, I'm putting the RE in curly
braces and not double quotes; that usually helps. Secondly, I'm putting
the magical (?n) flag in (if you want to know what it does, see
http://www.tcl.tk/man/tcl8.5/TclCmd/re_syntax.htm#M88 and also
http://www.tcl.tk/man/tcl8.5/TclCmd/re_syntax.htm#M95). Thirdly, I'm
noticing that the serial number is alphanumeric, allowing for a much
stricter match of the interesting information (i.e. the \w+ though I
don't know what Cisco serial numbers actually look like other than the
example you posted in your debugging trace). Apart from that, I've done
a bit of tidying up of your RE as well (as there's no point in capturing
a literal for its string value).

If you need more help on RE writing, you might be better off getting a
book that specializes on this topic and using that in conjunction with
the Expect book. And there's the online Tcl manual too, of course. :-)

Hope this helps.

Donal.

0 new messages