To sort all the results chronologically means that I would have to read them all in first (they could get quite large), perform the sort, and print out the output. I thought of the following alternative options:
Assuming each log file is in chronological order wouldn't it be easier to go through them in parallel, writing out the the entries in order as you go? Something like this:
# MAKE A FILEHANDLE FOR EACH FILE WE WERE GIVEN my @files = map {new IO::File $_ or die "could not open $_\n"} @ARGV; # READ IN A LINE FOR EACH FILE my @lines = map {scalar(<$_>)} @files; # GET THE DATES FOR EACH LINE; my @dates = map {get_time($_)} @lines; my $MAX = <some date bigger than anything in the logs>; my $found; do { # FIND THE LINE WITH THE EARLIEST DATE my $min = $MAX; $found = undef; for (my $i=0; $i<$num_logs; $i++) { my $num = $dates[$i]; if ($num < $min) { $found = $i; $min = $num; }; }; if (defined($found)) { # IF WE FOUND A LINE, SHOW IT AND READ THE NEXT # LINE IN FOR THAT LOG FILE print $lines[$found]; my $io = $files[$found]; $lines[$found] = <$io>; $dates[$found] = get_time($lines[$found]); }; } while (defined($found));
I did something similar to this to merge multiple apache access logs together a few years back.
In reply to Re: Log parsing by timestamp dilema
by adrianh
in thread Log parsing by timestamp dilema
by Limbic~Region
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |