Date::Parse is your friend for this sort of operation. Because you're using US format date, you can't do an simple (numeric or string wise) sort.
By preference, I'd say 'use the ISO 8601 standard date format' e.g. YYYY-MM-DD HH:MM::SS - the reason being because then this problem is trivial - it sorts both numerically and stringwise - but I realise that's not always an options, so instead:
use Date::Parse; my %sort_hash; my $line = "01/14/2014 23:44:12 <data1> <data2>"; my ( $datestr, $timestr, @rest_of_string ) = split ( /\s+/, $line ); my $unix_time = str2time ( $datestr . " " . $timestr ); print $unix_time,"\n"; $sort_hash{$unix_time} = join ( " ", @rest_of_string );
I'm sure you can adapt this for a 'while' loop easily enough.
In reply to Re: sorting logfiles by timestamp
by Preceptor
in thread sorting logfiles by timestamp
by jasonl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |