Last active
September 25, 2021 12:56
-
-
Save kanzure/cd2f7ae1f66743799f043b5cf5cb3882 to your computer and use it in GitHub Desktop.
Split an irssi IRC log file into multiple files (by date)
This file contains 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/python | |
#author: Bryan Bishop <[email protected]> | |
#date: 2010-03-16 | |
#updated: 2020-02-10 | |
from datetime import datetime | |
strptime = datetime.strptime | |
logs = open("irclogs.txt", "r").read().split("\n") | |
LOG_OPENED = "--- Log opened " | |
DAY_CHANGED = "--- Day changed " | |
# first line: --- Log opened Thu Dec 13 13:55:28 2018 | |
# format: %a %b %d %H:%M:%S %Y | |
#current = open("initial.log", "w") | |
current = None | |
for (idx, line) in enumerate(logs): | |
if idx == 0 and LOG_OPENED in line and line[0:len(LOG_OPENED)] == LOG_OPENED: | |
initial_date = strptime(line[len(LOG_OPENED):], "%a %b %d %H:%M:%S %Y") | |
current = open(initial_date.strftime("%Y-%m-%d") + ".log", "w") | |
elif DAY_CHANGED in line and line[0:len(DAY_CHANGED)] == DAY_CHANGED: | |
date = line[len(DAY_CHANGED):] | |
parsed_date = strptime(date, "%a %b %d %Y") | |
current.close() | |
current = open(parsed_date.strftime("%Y-%m-%d") + ".log", "w") | |
current.write(line + "\n") | |
current.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment