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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|