Printable Version | Subscribe | Add to Favourites
New Topic New Poll New Reply
Author: Subject: Any PERL experts in the house
mark chandler

posted on 27/11/10 at 09:02 PM Reply With Quote
Any PERL experts in the house

Years since I did PERL, and I want to extract some text from a Cisco routers config:

I am trying to extract the numbers after Speed (kbps):

MAS_ALDERSHOT_2811_001P#sh DSL int | include Speed
Speed (kbps): 0 11771 0 939
MAS_ALDERSHOT_2811_001P#qCC


Here's my code which I would expect to return the whole line

sub Parse_DLSWspeeds{
my @line=@_;
my ($ADSLspeed) = ($line[0] =~ /Speed (.*)/m) or die;
return $ADSLspeed;

but it just returns:

(kbps)

Anyone any idea what I need to pull in the whole line? it stops when it arrives at the ':' after (kbps)

Cheers Mark

[Edited on 27/11/10 by mark chandler]

View User's Profile View All Posts By User U2U Member
BenTyreman

posted on 27/11/10 at 09:47 PM Reply With Quote
A quick bit of Googleing suggests your regex should be
code:
m/Speed (.*)/
not
code:
/Speed (.*)/m


Don't know about the rest of the code, but it might be a start. Personally, I would have thought something like
code:
if ($line[0] =~ m/Speed (.*)/) {
return $1
} else {
return ""
}
is simpler.

[Edited on 27/11/10 by BenTyreman]

[Edited on 27/11/10 by BenTyreman]






View User's Profile E-Mail User View All Posts By User U2U Member
Mark Allanson

posted on 27/11/10 at 09:49 PM Reply With Quote
Wow - I understood all of that - not





If you can keep you head, whilst all others around you are losing theirs, you are not fully aware of the situation

View User's Profile Visit User's Homepage View All Posts By User U2U Member
mark chandler

posted on 27/11/10 at 10:27 PM Reply With Quote
Thanks Ben

Unfortunately the same result, its is still stopping at :

I cannot see why this is not being captured with .*

View User's Profile View All Posts By User U2U Member
BenTyreman

posted on 27/11/10 at 10:58 PM Reply With Quote
I have just tested this code
code:
#!/usr/bin/env perl

sub parse_speed {
if (shift =~ m/Speed (.*)/) {
return $1;
} else {
return "not found";
}
}

print(parse_speed('Speed (kbps): 0 11771 0 939'));
and it returns
code:
(kbps): 0 11771 0 939


Calling
code:
print(parse_speed('Spezzzed (kbps): 0 11771 0 939'));
returns
code:
not found


If this doesn't work on your system, then I have exhausted my knowledge of Perl.






View User's Profile E-Mail User View All Posts By User U2U Member
mark chandler

posted on 27/11/10 at 11:11 PM Reply With Quote
Wow thanks for trying Ben, unfortunately no banana, I think that the Cisco router must be returning the line in an odd format....

sub Parse_DLSWspeeds{
if (shift =~ m/Speed (.*)/) {
return $1;
} else {
return "not found";
}
}

Still returns (kbps) When its sucked from the router, it works correctly when sourced from a text file.

If I match with ^\s (which should ignore blank characters) then it returns not found which confirms this I think.

Boo

Regards Mark


[Edited on 27/11/10 by mark chandler]

View User's Profile View All Posts By User U2U Member
scudderfish

posted on 28/11/10 at 10:36 AM Reply With Quote
Or how about a bit of sh

cat tst.txt | grep "Speed (kbps):" | cut -f 2-5






View User's Profile View All Posts By User U2U Member
mark chandler

posted on 28/11/10 at 08:41 PM Reply With Quote
Thanks chaps

Got there in the end, reloaded perl.... and it started to behave as I expected. bugger 2 days wasted!

View User's Profile View All Posts By User U2U Member
ChrisW

posted on 28/11/10 at 11:15 PM Reply With Quote
If it was just the numbers you wanted, I'd have preffered:

if ($line =~ /Speed.*([0-9 ]+)/) {

$speed = $1;

}

Chris

View User's Profile E-Mail User Visit User's Homepage View All Posts By User U2U Member

New Topic New Poll New Reply


go to top






Website design and SEO by Studio Montage

All content © 2001-16 LocostBuilders. Reproduction prohibited
Opinions expressed in public posts are those of the author and do not necessarily represent
the views of other users or any member of the LocostBuilders team.
Running XMB 1.8 Partagium [© 2002 XMB Group] on Apache under CentOS Linux
Founded, built and operated by ChrisW.