Re: Loggfile extraction by date

From: David J N Begley <david@dont-contact.us>
Date: Tue, 3 Dec 1996 22:45:12 +1100 (EST)

On Tue, 3 Dec 1996, Mikel Lindsaar wrote:

> > spanning several months. Is there a way to extract only those entries
> > from one spesific month?
>
> If you are using unix a simple way would be

Alternatively (and somewhat more complex - this is what I use to split our
logs for processing "daily snapshots"):

 ---[ make way, perl goes here ]---

#! /usr/local/bin/perl -w

$dy = "";
$dm = "";
$dd = "";
$dx = "";
$ct = "";
%fh = {};

while( <> )
{
   ( $dx, $dx, $dx, $dd, $dm, $dy ) = localtime( substr( $_, 0, 9 ) );
   $ct = sprintf( "%04d%02d%02d", ($dy + 1900), ($dm + 1), $dd );
   if( ! exists $fh{$ct} )
   {
      $fh{$ct} = "";
      open( $fh{$ct}, ">>access.log.$ct" );
   }
   printf( {$fh{$ct}} "%s", $_ );
}

foreach $dx (keys %fh)
{
   close( $fh{$dx} );
}

 ---[ make way, perl goes here ]---

This currently splits on a day-by-day basis - there's no reason why this
couldn't be modified to create monthly snapshots instead. Useful when you
want to be sure that the "daily" stats you're processing cover midnight to
midnight, nothing before and nothing after. Use as a filter, piping the
"access.log" as input - the multiple files are generated as output (see
code above).

Cheers..

dave
Received on Tue Dec 03 1996 - 04:57:03 MST

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 16:33:49 MST