in reply to pattern matching
If you need to do anything other than display the results once you've isolated the uptime, you'll probably want a more useful value, such as seconds of uptime. Here's a script that will take an uptime statement and tell you the uptime in seconds. As with Stajich's solution, this may only work on uptimes of less than one year.
Have fun!#! /usr/bin/perl use strict ; use warnings ; use Parse::RecDescent ; my $rules = q{ startrule: current_time 'up' uptime remainder eol { $return = $item[3] ; } current_time: /\d+:\d+[AP]M/ uptime: days(?) time { $return = (@{$item[1]})[0] + $item[2] ; } days: number_days 'days,' { $return = $item[1] * 86_400 ; } number_days: /\d+/ time: hours ':' minutes { $return = $item[1] + $item[3] ; } hours: /\d+/ { $return = $item[1] * 3_600 ; } minutes: /\d+/ { $return = $item[1] * 60 ; } remainder: /.+/ eol: /^\Z/ } ; my $parser = new Parse::RecDescent( $rules ) ; my @tests = ( '11:22PM up 4:30, 2 users, load averages: 0.25, 0.42, 0.40', '6:37PM up 4 days, 2:05, 2 users, load averages: 1.99, 1.65, 1.47' ) ; foreach my $test ( @tests ) { print $test . ': ' . $parser->startrule( $test ) . "\n"; }
|
|---|