Skip to content

Instantly share code, notes, and snippets.

@bmaupin
Created January 19, 2016 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmaupin/eb6cda8fdaccad414090 to your computer and use it in GitHub Desktop.
Save bmaupin/eb6cda8fdaccad414090 to your computer and use it in GitHub Desktop.
Sort logs by /bin/date output
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