in reply to pattern matching
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'.#!/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"; }
HTH.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/);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: pattern matching
by Anonymous Monk on Jun 04, 2002 at 03:51 UTC |