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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.