Hi monks, I try to write a perl script that reads a log file, locate records which has same id number, but the time difference is more than 1 hour within the same day.

Herewith are the log file which was generated by a close source C program which was beyond my knowledge

ReadLog.txt

<b>2008-Feb-01 00:00:02 UTC (GMT +0000) - id = 000000001, Status = fai +led</b> 2008-Feb-01 00:10:02 UTC (GMT +0000) - id = 000000002, Status = succes +s <b>2008-Feb-01 00:20:02 UTC (GMT +0000) - id = 000000003, Status = fai +led</b> 2008-Feb-01 00:30:02 UTC (GMT +0000) - id = 000000004, Status = succes +s 2008-Feb-01 00:40:02 UTC (GMT +0000) - id = 000000001, Status = succes +s 2008-Feb-01 00:50:02 UTC (GMT +0000) - id = 000000001, Status = succes +s <b>2008-Feb-01 01:00:02 UTC (GMT +0000) - id = 000000001, Status = fai +led</b> 2008-Feb-01 01:10:02 UTC (GMT +0000) - id = 000000007, Status = succes +s <b>2008-Feb-01 01:20:02 UTC (GMT +0000) - id = 000000003, Status = fai +led</b> 2008-Feb-01 01:30:02 UTC (GMT +0000) - id = 000000009, Status = succes +s

ResultLog.txt

ID last attempted attempts ============================================================ 000000001 2008-Feb-01 01:00:02 1 000000003 2008-Feb-01 01:20:02 1


I tried the following but the output wasn't desirable.

use strict; use warnings; use DateTime::Format::Strptime; # my $Strp = new DateTime::Format::Strptime(pattern => '%Y-%b-%d % +T',); my $Strp = new DateTime::Format::Strptime(pattern => '%Y-%b-%d %T' +, on_error => 'croak'); my $infile = 'ReadLog.txt'; my $outfile = 'ReportLog.txt'; my($fh_out, $fh); my %lookup; my $status = 'failed'; my $time_delta = 3600; # seconds = 1 hour open($fh_out, '>', $outfile) or die "Could not open outfile: $!"; open($fh, '<', $infile) or die "Could not open logfile: $!"; while (<$fh>) { next unless /$status/; $_ =~ m/^(.*) UTC.*refs = (\d+)$/; # my $dt = $Strp->parse_datetime("$1"); unless (my $dt = $Strp->parse_datetime($1)) { warn "invalid datetime: $1"; next; } my $timestamp = $dt ->epoch(); my $refs = "$2"; if ( defined($lookup{$refs}) && $lookup{$refs} + $time_delta <= $t +imestamp ) { print $fh_out "REFS $refs: occurrences at " . $lookup{$refs} . + "and $timestamp \n"; print "REFS $refs: occurrences at " . $lookup{$refs} . " and $ +timestamp \n"; } $lookup{$refs} = $timestamp; } close $fh_out; #close $fh;

In reply to Logfile analysis : How to differentiate records with timestamp by tuakilan

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.