in reply to Re^3: sorting logfiles by timestamp
in thread sorting logfiles by timestamp
Here's essentially the same thing without Time::Piece.
use strict; use warnings; my @data; while (<DATA>) { my ($date, $time, $data1, $data2) = split; my $key = $date . $time; $key =~ s{^(\d\d)/(\d\d)/(\d\d\d\d)(\d\d):(\d\d):(\d\d)}{$3$1$2$4$ +5$6}; push @data, [ $key, join(',', $date, $time, $data1, $data2) ]; } print map { "$_->[1]\n" } sort { $a->[0] <=> $b->[0] } @data; __DATA__ 01/14/2014 23:44:14 A Y 01/14/2014 23:44:12 B Y 01/14/2014 23:44:13 C X 01/14/2014 23:44:12 D X
Better yet, skip split() and use a single regular expression to parse the whole log record as I did here. Personally, I'd take this opportunity to improve the log records by keeping the ISO 8601 format timestamps instead of just using them as a transitory sort key.
Even Dave Rolsky sanctions using a regular expression instead of a proper timestamp parser in exactly this kind of situation. See this slide in his presentation titled A Date with Perl, which you can watch him present here.
Jim
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: sorting logfiles by timestamp
by kcott (Archbishop) on Jan 22, 2014 at 14:37 UTC | |
by Jim (Curate) on Jan 22, 2014 at 18:04 UTC | |
by kcott (Archbishop) on Jan 23, 2014 at 13:59 UTC | |
by Jim (Curate) on Jan 24, 2014 at 16:50 UTC |