Hi Monks

I put together following codes were put together with help from fellow monks ( thanks alot ! ).

The idea of the task is to :

  • read a log file, in this case, log.yyyy-mm-dd
  • comb thru the log file to look for records that contains the string 'two' and duration between the 2 records is more than 3600 seconds.
  • export the result into a report file, report.yyyy-mm-dd.txt
  • I have done this far and when i run the code with, it created an file with zero byte size.

    Any clue what went wrong with the script ?

    #!/usr/local/bin/perl -w use strict; use warnings; use DateTime::Format::Strptime; my $Strp = new DateTime::Format::Strptime(pattern => '%Y-%b-%d %T' +,); my $infile = 'log.2008-01-11'; my $outfile = 'report.2008-01-11.txt'; my($fh_out, $fh); my %lookup; my $channel = 'two'; 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 /$channel/; my ($dt, $timestamp, $refs); if (m/^(.*) UTC.*ref .*? = (\d+)$/) { my $t = $1; $refs = $2; $dt = $Strp->parse_datetime($t); $timestamp = $dt->epoch(); warn "found: $. $timestamp\t$refs\n"; } else { warn "No match: $_ \n"; next; } 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;

    content of log.2008-01-11

    2007-Jan-11 00:00:00 UTC (GMT +0000) - Poll: channel = one, ref = com, + id = 595143009 2007-Jan-11 00:00:01 UTC (GMT +0000) - Poll: channel = two, ref = com, + id = 133714761 2007-Jan-11 00:00:01 UTC (GMT +0000) - Poll: channel = two, ref = com, + id = 595131400 2007-Jan-11 00:00:02 UTC (GMT +0000) - Poll: channel = three, ref = co +m, id = 660868931 2007-Jan-11 00:00:02 UTC (GMT +0000) - Poll: channel = two, ref = com, + id = 595191883 2007-Jan-11 00:00:03 UTC (GMT +0000) - Poll: channel = one, ref = com, + id = 098533326 2007-Jan-11 00:00:03 UTC (GMT +0000) - Poll: channel = three, ref = co +m, id = 659718092 2007-Jan-11 00:00:04 UTC (GMT +0000) - Poll: channel = two, ref = com, + id = 006456768 2007-Jan-11 00:00:05 UTC (GMT +0000) - Poll: channel = three, ref = co +m, id = 133714761 2007-Jan-11 01:07:06 UTC (GMT +0000) - Poll: channel = two, ref = com, + id = 133714761

    Desired contents of report.2008-01-11.txt

    2007-Jan-11 00:00:01 UTC (GMT +0000) - Poll: channel = two, ref = com, + id = 133714761 0 2007-Jan-11 00:00:01 UTC (GMT +0000) - Poll: channel = two, ref = com, + id = 595131400 0 2007-Jan-11 00:00:02 UTC (GMT +0000) - Poll: channel = two, ref = com, + id = 595191883 0 2007-Jan-11 00:00:04 UTC (GMT +0000) - Poll: channel = two, ref = com, + id = 006456768 0 2007-Jan-11 01:07:06 UTC (GMT +0000) - Poll: channel = two, ref = com, + id = 133714761 1

    In reply to failure to generate report file 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.