Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Sorting a logfile by date to print to html

by cjensen (Sexton)
on Oct 09, 2001 at 21:50 UTC ( [id://117816]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://117816]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-19 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found