in reply to Sorting apache log files
I personally wouldn't bother with all that unneccessary time consuming Date::Calc nonsense, and if you do that, you wouldn't have to bother with the custom compare routine.#!/usr/bin/perl -w use strict; use Fcntl; use DB_File; use Date::Calc qw|Date_to_Time Decode_Month|; my $filename = __FILE__.".${$}.db"; # $DB_BTREE is exported by DB_File # it is a sorted balanced binary tree # we enable duplicate keys $DB_BTREE->{'flags'} = R_DUP ; # since default sorting is lexical, and we want numeric $DB_BTREE->{'compare'} = sub { my( $keyA, $keyB ) = @_; return $keyA <=> $keyB; }; my $X = tie my(%H), "DB_File", $filename, O_RDWR|O_CREAT|O_TRUNC, 0666, $DB_BTREE or die "Cannot open $filename: $!\n"; foreach $file (@ARGV) { die("This is not a file: $!") unless (-f $file); open(FILE, $file) or die "WTF? couldn't open $file cause $!"; while(<FILE>){ chomp; my $date = get_timestamp($_); $H{$date} = $_; } close(FILE); } # iterate through the btree using seq # and print each key/value pair. my $key = 0; my $value = 0 ; my $status = ""; for( $status = $x->seq($key, $value, R_FIRST) ; $status == 0 ; $status = $x->seq($key, $value, R_NEXT) ) { print "$value\n"; } undef $X ; untie %H ; # delete our temporary database unlink $filename or die "couldn't delete $filename cause $!"; exit; ## subland sub get_timestamp { my $line = shift; $line =~ /\[(.*) -\d+\]/; my $tempdate = $1; my ($day,$mon,$year,$hour,$min,$sec) = $tempdate =~ /(\d+)\/(\w+)\/(\d+):(\d+):(\d+):(\d+)/; $mon = Decode_Month($mon); my $stamp = Date_to_Time($year,$mon,$day,$hour,$min,$sec); }
On another note, your get_timestamp sub has a classic logic flaw, you assign $1 to something, without ever knowing if there was a match. You should if(/(match)){ $foo = $1; ... }
____________________________________________________
** The Third rule of perl club is a statement of fact: pod is sexy.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Sorting apache log files
by rlb3 (Deacon) on Oct 01, 2002 at 05:46 UTC |