Created
May 31, 2013 23:14
-
-
Save ryands/5688570 to your computer and use it in GitHub Desktop.
IRSSI log concatenation, sort by date from multiple input files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# usage: ./logconcat.py output.log [list, of, input, files] | |
# Eventually I'll put all log entries in a db for more fun. | |
from datetime import datetime | |
class LogEntry(): | |
def __init__(self, date, entry): | |
self.date = date | |
self.entry = entry | |
def build_log(output, files): | |
buf = [] | |
curdate = None | |
for f in files: | |
fh = open(f, 'r') | |
for line in fh: | |
if line.startswith('--- Day changed'): | |
stamp = line[len('--- Day changed')+1:].strip() | |
print stamp | |
curdate = datetime.strptime(stamp, '%a %b %d %Y') | |
elif line.startswith('--- Log opened'): | |
stamp = line[len('--- Log opened')+1:].strip() | |
print stamp | |
curdate = datetime.strptime(stamp, '%a %b %d %H:%M:%S %Y') | |
buf.append(LogEntry(curdate, line)) | |
fh.close() | |
buf = sorted(buf, key=lambda x: x.date) | |
out = open(output, 'w') | |
for line in buf: | |
out.write(line.entry) | |
out.write('\n') | |
out.close() | |
if __name__ == '__main__': | |
import sys | |
build_log(sys.argv[1], sys.argv[2:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment