⇤ ← Revision 1 as of 2013-04-09 11:42:13
1078
Comment:
|
← Revision 2 as of 2021-04-08 03:07:34 ⇥
1106
|
Deletions are marked like this. | Additions are marked like this. |
Line 6: | Line 6: |
* Links [[Linux\Rsyslog]] |
Split syslog file in python
Links Linux\Rsyslog
- 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)[7] 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)[7] for line in wwwlog) bytes = (int(x) for x in bytecolumn if x != '-') print "Total", sum(bytes)
...