#!/usr/bin/env python from os import listdir from os.path import isfile, join from datetime import datetime # Set the dir you'd like to eat, the suffix of each file, and the filename to shit into dir_to_eat = '/path/to/eat/dir/' dir_files_end_with = '.log' shit_into = '/path/to/shitfile.txt' # Set the top limit of the line index archive_top_inx = 999999 # Leave these variables alone archive_bottom_inx = 0 dir_data = [f for f in listdir(dir_to_eat) if isfile(join(dir_to_eat, f))] logline_array = [] # Sort the dir_data in chronological order date_format = '%Y-%m-%d' dir_data_chrono = [] for logfile in dir_data: if dir_files_end_with in logfile: logdate = logfile.replace('.log','') dir_data_chrono.append(logdate) dir_data_chrono = sorted(dir_data_chrono, key=lambda d:datetime.strptime(d,date_format)) # Shit lines into a single file def shit_lines(line_inx): f = open(shit_into, 'a') for line in logline_array: indexed_line = ('%s;%s') % (line_inx,line) f.write(indexed_line) line_inx = line_inx + 1 f.close() # Eat each file in the dir for logfile in dir_data_chrono: logfile_full_path = join(dir_to_eat,('%s.log' % logfile)) with open(logfile_full_path, 'r') as logfile_data: for logline in logfile_data: znc_timestamp = ('%s %s') % (logfile, logline[1:9]) znc_datetimestamp = datetime.strptime(znc_timestamp, '%Y-%m-%d %H:%M:%S') epoch_datetimestamp = znc_datetimestamp.strftime('%s') if '<' in logline[11:12]: #normal line speaker = logline[12:logline.find('>')] logline_array.append(('%s;%s;%s') % (epoch_datetimestamp, speaker, logline[logline.find('>')+2:])) elif '*' in logline[12:13]: #connectolade; ignore pass else: #action logline_array.append(('%s;*;%s') % (epoch_datetimestamp, logline[13:])) archive_bottom_inx = archive_top_inx - (len(logline_array) - 1) shit_lines(archive_bottom_inx) logline_array = []