#!/usr/bin/perl
#   logsort.pl 1.3 2009-08-03 14:04:37-04 davidsen Testing
# sort log lines based on the date at the end of the line
#   author Bill Davidsen, <davidsen@tmr.com>

# define strings starting a day of the week
$DayStr = "Sun|Sunday|Mon|Monday|Tue|Tuesday|Wed|Wednesday|Thu|Thursday|Fri|Friday|Sat|Saturday";
# the temp filename
$TFN = ".ds";

# open a temp file for sort
open(TEMP, ">${TFN}") || die "open temp";

while (<>) {
	# get the date portion of the line
	if (m/.*\s(${DayStr}),? (.*)/) {
		($dp = $2) ;
		# trace / debug code deleted...
	}
	# save the text indexed by the date
	#   may be multiple entries
	$text{$dp} .= $_;
}
print TEMP join("\n", keys %text);
close TEMP;

# sort the indices
open(TEMP, "sort -M ${TFN}|") || die "reopen";
while (<TEMP>) {
	chomp;
	print "$text{$_}";
}

close TEMP;
unlink ${TFN};

exit 0;
