Sorting Apache Log Files

Richard van de Stadt stadt at cs.utwente.nl
Mon Jun 25 18:21:01 EDT 2001


Lenny Self wrote:
> 
> Thanks for your help.  This is what I ended up doing... It seems to work
> quite nicely and seems fast enough.  Although, I'm not sure how fast its
> going to be with 20MB of logs :)
> 
> #!/usr/bin/pyton
> 
> import string
> 
> # Reading file into list
> list = open("d:/work/access.log","r").readlines()
> def compare (line1,line2):
>     # Nicely sucks out the apache date stamp
>     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:
[...]

That only works in case you have 2 log files from the same month.

>>> date1='12/Jan/2001:11:30:31 -0500'
>>> date2='01/Feb/2001:00:02:47 -0500'
>>> date1 < date2
0
>>> date2 < date1
1
>>> 

Or does your log contain dates in this format:
2001/01/12:11:30:31

In that case, it works.

>>> date1='2001/01/12:11:30:31'
>>> date2='2001/02/01:00:02:47'
>>> date1 < date2
1
>>> 

Richard.



More information about the Python-list mailing list