Created
April 22, 2016 12:24
-
-
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
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
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