Created
January 19, 2016 19:05
-
-
Save bmaupin/eb6cda8fdaccad414090 to your computer and use it in GitHub Desktop.
Sort logs by /bin/date output
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
text = '''Tue Jan 19 14:00:42 EST 2016 | |
real 0m2.491s | |
Sun Jan 17 21:46:41 EST 2016 | |
real 0m15.013s | |
Mon Jan 18 00:49:33 EST 2016 | |
real 0m15.039s | |
Sun Jan 17 23:49:16 EST 2016 | |
real 0m15.024s | |
Mon Jan 18 02:50:38 EST 2016 | |
real 0m15.020s | |
Tue Jan 19 11:58:42 EST 2016 | |
real 0m17.481s''' | |
lines = [] | |
prev_line = '' | |
for line in text.split('\n'): | |
if line.startswith('real'): | |
lines.append('{}\n{}'.format(prev_line, line)) | |
else: | |
prev_line = line | |
def lines_sort_func(line): | |
return datetime.datetime.strptime(line.split('\n')[0], '%a %b %d %H:%M:%S %Z %Y') | |
lines.sort(key=lines_sort_func) | |
for line in lines: print(line) | |
''' | |
Sun Jan 17 21:46:41 EST 2016 | |
real0m15.013s | |
Sun Jan 17 23:49:16 EST 2016 | |
real0m15.024s | |
Mon Jan 18 00:49:33 EST 2016 | |
real0m15.039s | |
Mon Jan 18 02:50:38 EST 2016 | |
real0m15.020s | |
Tue Jan 19 11:58:42 EST 2016 | |
real0m17.481s | |
Tue Jan 19 14:00:42 EST 2016 | |
real0m2.491s | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment