This should get you going - I'm not sure what happens when uptime > 365 days if it converts to years too. I'll have to login into one of my UN*X boxes to see =)
#!/usr/bin/perl -w
use strict;
# test on a string with just hours
my $uptime = ' 9:12pm up 2:13, 5 users, load average: 0.84, 0.62,
+ 1.03';
if( $uptime =~ /up\s+((\d+) days,\s+)?(\S+),/ ) {
my ($dayup,$timeup) = ( $2,$3); # $1 is the first enclosing parens
+ which we don't want
print "timeup is ", defined $dayup ? " $dayup days and " : '', "$t
+imeup hours\n";
}
# test again on a string with days
$uptime = "6:37PM up 4 days, 2:05, 2 users, load averages: 1.99, 1.65,
+ 1.47";
if( $uptime =~ /up\s+((\d+) days,\s+)?(\S+),/ ) {
my ($dayup,$timeup) = ( $2,$3); # $1 is the first enclosing parens
+ which we don't want
print "timeup is ", defined $dayup ? " $dayup days and " : '', "$t
+imeup hours\n";
}
Another way to attack this if you aren't comfortable with those regexps is to strip away the things you know you don't need and just get what you know you want ... Everything between 'up' and 'XX users'.
my $uptime = ' 9:12pm up 2:13, 5 users, load average: 0.84, 0.62,
+ 1.03';
my ($keep) = ( $uptime =~ /up\s+(.+),\s+\d+\s+users/);
HTH. |