I'm going to take stab at what I think you are doing - correct me if I don't have it right.

It seems that you are monitoring a log file, and you want to split the data into hourly log files (and perhaps perform some processing on them, too.)

Assuming this is what you want to do, here's my approach:

my ($t0, $path0, $out); $t0 = time; $path0 = path_for_time($t0); open($out, ">>", $path0); # note below while (defined(my $line = readline())) { my $t = time; my $path1 = path_for_time($t); if ($path1 ne $path0) { close($out); $path0 = $path1; open($out, ">>", $path0); } print $out $line; flush($out); # note below ... perform other processing on $line ... }
All you need to supply is the path_for_time() subroutine.

The routine readline() can just read from a pipe from tail as it seems like you are doing or you can use File::Tail to move the functionality into the perl script itself. Also, I use open the output files in append mode and call flush() in case you ever make this script restartable someday.

Is this what you want to do?


In reply to Re: Splitting up a file in hourly increments by pc88mxer
in thread Splitting up a file in hourly increments by hallikpapa

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.