tuakilan has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I piece together the following codes thanks to the gracious time and effort of fellow monks in this forum.

the code works but it generated a zero byte output file without the results of records having the string 'two'

#!/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 = 'logfile-2008-01-11.log'; 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/; # $_ =~ m/^(.*) UTC.*refs = (\d+)$/; # my $dt = $Strp->parse_datetime("$1"); # my $timestamp = $dt ->epoch(); # my $refs = "$2"; my ($dt, $timestamp, $refs); if ( m/^(.*) UTC.*refs = (\d+)$/ ){ # right here, just after the match, is the only # place it's safe to use $1 and $2 - tuck them away: my $t = $1; $refs = $2; $dt = $Strp->parse_datetime($t); $timestamp = $dt ->epoch(); } 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;

The input file, logfile-2008-01-11.log

2007-Jan-11 00:00:00 UTC (GMT +0000) - Poll: channel = one, ref = com, + id = 595143009 2007-Jan-07 00:00:01 UTC (GMT +0000) - Poll: channel = two, ref = com, + id = 133714761 2007-Jan-11 00:00:01 UTC (GMT +0000) - Poll: channel = one, ref = com, + id = 595131400 2007-Jan-11 00:00:02 UTC (GMT +0000) - Poll: channel = two, ref = com, + 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 = three, ref = co +m, id = 006456768 2007-Jan-11 00:00:05 UTC (GMT +0000) - Poll: channel = one, ref = com, + id = 133714761

The desired output file, report-2008-01-11.txt

If the record has more than 1 repetition, the output file shall have the character '1' added to the last position

2007-Jan-11 00:00:05 UTC (GMT +0000) - Poll: channel = one, ref = com, + id = 133714761 1
** Updated ** as requested

Replies are listed 'Best First'.
Re: output the read result into a log file
by moritz (Cardinal) on Mar 04, 2008 at 07:45 UTC
    So what do you want the output to look like?

    And what's the input?

    It's hard to help you if all you provide us is "this program doesn't work, it produces nearly empty output".

    We can't read your mind, and even if you provided examples of the input files in a different thread, you can't expect us to remember it.

      updated as requested
        Better. Now it is evident that
        if (m/^(.*) UTC.*refs = (\d+)$/) { ^^^^
        does not match
        2007-Jan-07 00:00:01 UTC (GMT +0000) - Poll: channel = two, ref = com, + id = 133714761

        Try
        # slightly altered--v if (m/^(.*) UTC.*ref .*? = (\d+)$/) { my $t = $1; $refs = $2; $dt = $Strp->parse_datetime($t); $timestamp = $dt->epoch(); warn "found: $. $timestamp\t$refs\n"; }
        That seems to mach, and will tell you about it. Now, if that does not go to your file, it will have to do with later steps.
Re: output the read result into a log file
by stiller (Friar) on Mar 04, 2008 at 08:06 UTC
    Hi, you are probably almost there now, but it's kind of hard to tell why your output file is empty, because we can't run this without reasonable data.

    You can provide a few lines from the log at the end of your program:

    __DATA__ log lines here and here a couple that should be ignored and a couple you think should be proccessed

    Comment out the line that opens the logfile, and replace
    while (<$fh>) {
    with
    while (<DATA>) {
    to read from the embedded logdata. Also, delete lines you have commented out that is no longer going to be used. If you want, keep them in some document for reference, but get the clutter away.

    cheers

Re: output the read result into a log file
by poolpi (Hermit) on Mar 04, 2008 at 12:29 UTC

    In your example,
    the script always find just one line with the 'two' keyword and the same id from the log file.

    So you never go inside the

    if ( defined($lookup{$refs}) && $lookup{$refs} + $time_delta <= $t +imestamp )
    statement and there is no output.

    And $lookup{$refs} + $time_delta is always greater than $timestamp

    Maybe it's different in your real log file ?

    hth,

    PooLpi