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]
A quick bit of Googleing suggests your regex should be
notcode:
m/Speed (.*)/
code:
/Speed (.*)/m
is simpler.code:
if ($line[0] =~ m/Speed (.*)/) {
return $1
} else {
return ""
}
Wow - I understood all of that - not
Thanks Ben
Unfortunately the same result, its is still stopping at :
I cannot see why this is not being captured with .*
I have just tested this code
and it returnscode:
#!/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'));
code:
(kbps): 0 11771 0 939
returnscode:
print(parse_speed('Spezzzed (kbps): 0 11771 0 939'));
code:
not found
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]
Or how about a bit of sh
cat tst.txt | grep "Speed (kbps):" | cut -f 2-5
Thanks chaps
Got there in the end, reloaded perl.... and it started to behave as I expected. bugger 2 days wasted!
If it was just the numbers you wanted, I'd have preffered:
if ($line =~ /Speed.*([0-9 ]+)/) {
$speed = $1;
}
Chris