· 8 years ago · May 23, 2018, 11:22 PM
1import fileinput
2import re
3from time import strptime
4from glob import glob
5
6__author__ = "Zohar Yzgeav"
7__email__ = "zohar.yzgeav at gmail.com"
8
9
10log_files = ['log1.txt', 'log2.txt', 'log3.txt']
11# if we want to parse & unify all of the current log files we could follow:
12# log_files = [f for f in glob('*.txt') if 'log' in f and f != 'unified_log.txt']
13
14
15def merge_log_files():
16 """ Produce a unified log which combines all of the current directory log files """
17 global log_files
18 lines = list(fileinput.input(log_files))
19 t_fmt = '%H%M.%S' # format of time stamp
20 t_pat = re.compile(r'<(.*?)>')
21
22 for line in sorted(lines, key=lambda line: strptime(
23 t_pat.search(line).group(1), t_fmt)):
24 yield line
25
26if __name__ == "__main__":
27 unified_log = open("unified_log.txt", "w")
28 for line in merge_log_files():
29 unified_log.write(line)
30 merge_log_files().next()
31 unified_log.close()