Ben Tarikau Ben Tarikau - 2 months ago 8x
Apache Configuration Question

Sortin apache log with Python

For example I have this simple apache log: GET /index.php GET /pilt.png GET /index.php GET /index.php GET /pilt.png GET /index.php GET /index.php GET /index.php GET /kaust/index.php GET /index.php

How would I write a Python code to sort it out that all the similar IP addresses are together and counted how much IP addresses are there

w = open("C:\\Users\\xxx\\Desktop\\test.txt","r")

for i in w:
log=i.split(' ')
print log[0]

Have tried so much, but can't write the code further.

Thank you!


This is how it will be done :

x = open('PATH_TO_FILE').read()

from itertools import groupby
from operator import itemgetter
x = x.split('\n')
for i in range(len(x)):
    x[i] = x[i].split(' ')

j = 0

for elt, items in groupby(x, itemgetter(0)):
    j += 1
    k = 0
    print elt, items
    for i in items:
        k += 1
        print i
    print 'Total count for IP ',i[0],' is :',k

print 'Total unique IP address are : ',j