mark chandler
|
posted on 27/11/10 at 09:02 PM |
|
|
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]
|
|
|
BenTyreman
|
posted on 27/11/10 at 09:47 PM |
|
|
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]
|
|
Mark Allanson
|
posted on 27/11/10 at 09:49 PM |
|
|
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
|
|
mark chandler
|
posted on 27/11/10 at 10:27 PM |
|
|
Thanks Ben
Unfortunately the same result, its is still stopping at :
I cannot see why this is not being captured with .*
|
|
BenTyreman
|
posted on 27/11/10 at 10:58 PM |
|
|
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.
|
|
mark chandler
|
posted on 27/11/10 at 11:11 PM |
|
|
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]
|
|
scudderfish
|
posted on 28/11/10 at 10:36 AM |
|
|
Or how about a bit of sh
cat tst.txt | grep "Speed (kbps):" | cut -f 2-5
|
|
mark chandler
|
posted on 28/11/10 at 08:41 PM |
|
|
Thanks chaps
Got there in the end, reloaded perl.... and it started to behave as I expected. bugger 2 days wasted!
|
|
ChrisW
|
posted on 28/11/10 at 11:15 PM |
|
|
If it was just the numbers you wanted, I'd have preffered:
if ($line =~ /Speed.*([0-9 ]+)/) {
$speed = $1;
}
Chris
|
|