Split syslog file in python
- Problem is some of the fields are sentences.
used http://rtoodtoo.net/2012/03/06/how-to-preserve-quoted-strings-in-split-in-python/ as starting point
- Non generater version.
# nongenlog.py # # Sum up the number of bytes transferred in an Apache log file # using a simple for-loop. We're not using generators here. import shlex wwwlog = open("/var/log/apache2/access.log") total = 0 for line in wwwlog: bytestr = shlex.split(line) if bytestr != '-': total += int(bytestr) print "Total", total
- use python generator
# genlog.py # # Sum up the bytes transferred in an Apache server log using # generator expressions import shlex wwwlog = open("/var/log/apache2/access.log") bytecolumn = (shlex.split(line) for line in wwwlog) bytes = (int(x) for x in bytecolumn if x != '-') print "Total", sum(bytes)