Skip to content

Instantly share code, notes, and snippets.

@prakhar1989
Last active August 29, 2015 14:03
Show Gist options
  • Save prakhar1989/3557bc082bfdf57656b8 to your computer and use it in GitHub Desktop.
Save prakhar1989/3557bc082bfdf57656b8 to your computer and use it in GitHub Desktop.
Log Analysis
#!/usr/bin/python2.7
import re
from collections import Counter
import sys
def do_analysis(data):
date_map = dict()
for l in data:
date_patt = re.compile("\[(.*)\]")
date_str = date_patt.findall(l)[0][:20]
(day, month, hour, minute) = (date_str[:2], date_str[3:6], date_str[12:14], date_str[15:17])
if (day + month) in date_map:
date_map[day + month].append(hour + minute)
else:
date_map[day + month] = [hour + minute]
for date in date_map:
c = Counter(date_map[date])
for t in c:
print "%s, %d" % (t, c[t])
if __name__ == "__main__":
data = sys.stdin.readlines()
do_analysis(data)
@prakhar1989
Copy link
Author

Get file URL by clicking on the Raw link

$ wget file_url
$ chmod +x log_analysis.py
$ grep "29/Jun" site-access.log | ./log_analysis.py | sort -nk1 > 29_output.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment