Alex Rivera | Logout

How can I sort an Apache log file by date?

Asked 2011-04-15T05:31:29.090
19

I have a a couple of Apache log files that have been appended together and I need to sort them by date. They're in the following format:

"www.company.com" 192.168.1.1 [01/Jan/2011:00:04:17 +0000] "GET /foobar/servlet/partner/search/results?catID=1158395&country=10190&id=5848716&order_by=N-T&order_by_dir=-&product=10361996&siteID=1169823&state= HTTP/1.1" 200 10459 0 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"

What's the best way to go about doing this on the Linux command line?

Edit
Report

1 Answer

11

This is almost too trivial to point out, but just in case it confuses anyone: grm's answer should technically be using field #3, not 4, to match the questioner's exact log format. That is, it should read:

    sort -t ' ' -k 3.9,3.12n -k 3.5,3.7M ...

His answer is correct in every other respect, and can be used as-is for the common log format.

answered 2012-03-14T19:57:23.717

Your Answer