Here's an almost complete solution:
#!perl use strict; use warnings; my (%log, @results); while (<DATA>) { my ($mode, $user, $jobid, $timestamp) = (split)[0, 1, 2, 7]; if ($mode eq '<') { $log{$user.$jobid} = $timestamp; } else { push @results, [$user, $jobid, calc_runtime($log{$user.$jobid} +, $timestamp)]; delete $log{$user.$jobid}; } } for (@results) { printf "%s - JobID: %s - Runtime: %s\n", @$_; } sub calc_runtime { my ($start, $end) = @_; return qq{$end - $start}; } __DATA__ < root 26144 c Tue Nov 2 03:10:02 2010 < oracle 26161 c Tue Nov 2 03:10:25 2010 < oracle 26193 c Tue Nov 2 03:10:30 2010 < sybase 26163 c Tue Nov 2 03:10:32 2010 > oracle 26161 c Tue Nov 2 03:10:33 2010 < sybase 26188 c Tue Nov 2 03:10:38 2010 > sybase 26163 c Tue Nov 2 03:10:58 2010
Which outputs:
$ cron_log_prob.pl oracle - JobID: 26161 - Runtime: 03:10:33 - 03:10:25 sybase - JobID: 26163 - Runtime: 03:10:58 - 03:10:32
I'll leave you to pick apart the timestamps and do the calculation in calc_runtime(). If all your jobs are guaranteed to run for less than 24 hours, you can just check if the start time is later than the end time (indicating it ran over midnight) and do the appropriate arithmetic. If they're going to run for longer than 24 hours, you'll need to capture more time info but the process remains the same.
Update: I forgot you only wanted oracle and sybase jobs. Rather than using a regex with alternation (which may become rather unwieldy - and slow - if you need several users) try the following with a hash:
... my %wanted_user = map { $_ => 1 } qw{oracle sybase}; while (<DATA>) { my ($mode, $user, $jobid, $timestamp) = (split)[0, 1, 2, 7]; next if not $wanted_user{$user}; ...
-- Ken
In reply to Re: hash array
by kcott
in thread hash array
by roadtest
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |