Skip to content

Instantly share code, notes, and snippets.

@kanzure
Last active September 25, 2021 12:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanzure/cd2f7ae1f66743799f043b5cf5cb3882 to your computer and use it in GitHub Desktop.
Save kanzure/cd2f7ae1f66743799f043b5cf5cb3882 to your computer and use it in GitHub Desktop.
Split an irssi IRC log file into multiple files (by date)
#!/usr/bin/python
#author: Bryan Bishop <kanzure@gmail.com>
#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