in reply to Sorting a logfile by date to print to html

Your date format isn't perfect for sorting, but you can pipe your file through the unix sort command using ~ as your separator, on the fourth (0, 1, 2, 3) column, ignoring blank spaces:

sort -b -t '~' +3

That's assuming your OS has a compatible 'sort'.

You can reverse it for descending order:

sort -b -r -t '~' +3

It would be better if date was in a fully sortable format like yyyymmdd, as referenced above, or even yy/mm/dd

How about this mess:

cat <log file> | \
perl -p -e 's|(\d\d)/(\d\d)/(\d\d)|$3/$1/$2|' | \
sort -r -b -t '~' +3 | \
perl -p -e 's|(\d\d)/(\d\d)/(\d\d)|$2/$3/$1|'

Note: You can do a lot on the command-line, but it's not always the best way to do things.

If you want to parse a few decades of historical data and aren't worried about your script being around in 50 years or so, you can go way overboard:

cat <log file> | \
perl -p -e 's|(\d\d)/(\d\d)/(\d\d)|($3 > 50 ? "19$3" : "20$3")."$1$2"|e' | \
sort -r -b -t '~' +3 | \
perl -p -e 's|(\d\d)(\d\d)(\d\d)(\d\d)|$3/$4/$2|'

  • Comment on Re: Sorting a logfile by date to print to html