Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting input using @variable into a string

Status
Not open for further replies.

gindelhe

Technical User
May 23, 2002
9
US
Hello.

Sorry if the title doesn't make sense. I have some code that grabs a command from a router and puts it into an @variable. My question is I want to only grab a certain part of the @variable. Here is some of the code:

@output2 = $fw->cmd('show conn count');
print @output2;

Which would show something like this:
123456 in use, 123456 most used

What I need to do is to grab the number 123456 bolded in use to use for a test condition. I also have another command that I need to do the same with but the numbers are
in a different order. Here is another snippet:

@output1 = $fw->cmd('show memory');
print @output1;

which would show something like this:
123456789 byte total, 123456789 in use

In this example I need to grab the bolded set of numbers and write it to a variable or a string.

Is this enough or do I need to expand on what I need? I did try to do a search but I'm not 100 percent sure what to search for.

Thanks,
Hal.
 
Since the info you want is always followed by the phrase "in use", use a regex to capture the digits before this phrase and store it into a variable. Example:
Code:
#!perl
use strict;
my $in_use;
my @output2 ="123456 in use, 123456 most used";
if ($output2[0] =~ /[b](\d+)\s+in use[/b]/) {
    $in_use = $1;
}
print qq(\$in_use from \@output2: $in_use\n);

my @output1 = "123456789 byte total, 123456789 in use";
if ($output1[0] =~ /[b](\d+)\s+in use[/b]/) {
    $in_use = $1;
}
print qq(\$in_use from \@output1: $in_use\n);

Outputs:
$in_use from @output2: 123456
$in_use from @output1: 123456789
If you know that the number you want will be at the front or end of the string, you can optimize a bit with the '^' and $ line anchors.
Code:
# If we know it's at the front
if ($output2[0] =~ /[b]^\s*(\d+)\s+in use[/b]/) {
    $in_use = $1;
}

# If we know it's at the end
if ($output1[0] =~ /[b](\d+)\s+in use\s*$[/b]/) {
    $in_use = $1;
}
I've added \s* (zero or more whitespace chars) in case there's whitepace at the beginning or end of the string.
 
Okay I made on mistake I'm not very good at regex yet. The second command is in the following format:
123456789 bytes total, 123456789 bytes free

I need to grab the numbers bolded. Sorry about that mistake!

This code below works great.
Code:
# If we know it's at the front
if ($output2[0] =~ /^\s*(\d+)\s+in use/) {
    $in_use = $1;
}
This doesn't but here is what i tried to do an it didn't work:
Original Code:
Code:
# If we know it's at the end
if ($output1[0] =~ /(\d+)\s+in use\s*$/) {
    $in_use = $1;
}
Modified code:
Code:
# If we know it's at the end
if ($output1[0] =~ /(\d+)\s+free\s*$/) {
    $in_use = $1;
}
So what am I doing wrong?

Thanks,
Hal
 
In your modified code, you just left out the word bytes
Code:
# If we know it's at the end
if ($output1[0] =~ /(\d+)\s+[b]bytes[/b] free\s*$/) {
    $in_use = $1;
}
 
After some trail and error I am getting what I need. Thanks for your help Mikevh! That worked great and I learned a lot.

Hal.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top