Sorting Apache Log Files

Steve Holden sholden at holdenweb.com
Thu Jun 28 14:40:03 EDT 2001


Note, however, that you only get strptime() if your C library defines it.
There's a pure-Python implementation, but for more general usage you can't
beat the (free) mxDateTime module. See

    www.egenix.com

regards
 Steve
--
http://www.holdenweb.com/


"David Kirtley" <dkirtley at panam.edu> wrote in message
news:1ca31bad.0106271243.58052b7 at posting.google.com...
> > Lenny Self wrote:
>
> >>> SNIP <<<
>
> > import string
> >
> >   datestamp1 = line1[string.find(line1,"[") + 1:string.rfind(line1,"]")]
> >   datestamp2 = line2[string.find(line2,"[") + 1:string.rfind(line2,"]")]
> >   # Compare the date stamps and return appropriate value
> >   if datestamp1 < datestamp2:
> >           return -1
> >   elif datestamp2 < datestamp1:
> >           return 1
> >   else:
> >           return 0
> >ist.sort(compare)
> > Writing sorted list to new file
> >pen("d:/work/newfile.txt","w").writelines(list)
>
> That won't work like you think it will. That will do a lexographic
> comparison (alphabetical) that will partially work but not when they
> are on boundiries (worst case example: Jan < Apr  works but Jul < Aug
> doesnt.)
>
> The way to handle it is to convert it to a numeric time value:
>
> for the default Apache log format::  00/Mon/0000/00:00:00
>
>
> from time import *
>
> timestamp = "00/Mon/0000/00:00:00" #<- insert a real timestamp here.
> myTime = mktime(strptime(timestamp,"%d/%b/%Y:%H:%M:%S"))
>
> Now you can use myTime to make the comparison.
>
> David Kirtley





More information about the Python-list mailing list