Skip to content

Instantly share code, notes, and snippets.

@valsteen
Created April 22, 2016 12:24
Show Gist options
  • Save valsteen/edbd0ac76ecc2c7b89b4e1d5fa5292df to your computer and use it in GitHub Desktop.
Save valsteen/edbd0ac76ecc2c7b89b4e1d5fa5292df to your computer and use it in GitHub Desktop.
stupid utility that receives timestamped lines in any order and various formats, then outputs them reordered
import dateutil.parser
import re
import sys
import pytz
tz = pytz.timezone("Europe/Vienna")
res = []
for line in sys.stdin:
tsmatch = re.search("[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]", line)
if not tsmatch:
tsmatch = re.search("[0-9][0-9]\/[A-Za-z]+?\/[0-9][0-9][0-9][0-9]:[0-9][0-9]:[0-9][0-9]:[0-9][0-9] \+?[0-9]+", line)
if tsmatch:
line = re.sub("^(.*?[0-9][0-9][0-9][0-9]):([0-9][0-9].+)", "\\1 \\2", line)
tsmatch = re.search("[0-9][0-9]\/[A-Za-z]+?\/[0-9][0-9][0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] \+?[0-9]+", line)
if tsmatch:
ts = dateutil.parser.parse(tsmatch.group(0))
if not ts.tzinfo:
ts = tz.localize(ts, None)
res.append((ts, line))
print "".join([l[1] for l in sorted(res)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment