Firstly, I'm very new to Python and programming in general so please bear with me if this is a stupidly obvious question.
I have an undefined amount (possibly 10+) of log files mixed with other random files in a directory, and I need to merge these into a single file with the lines sorted by the time stamp at the beginning of each line. The log files are .txt and there are other non-log .txt files in the same directory so I'm going to just make the user of this script enter every log file as an argument.
Now before you mark this as a duplicate, I looked through 4 pages of search results on here and none of the questions have an answer I can use.
So far, I have the following sort-of working Python code:
log_file_name = 'logfile.txt'
import sys
import fileinput
from Tkinter import Tk
from tkFileDialog import askopenfilenames
logfile = open(log_file_name, 'w+')
logfile.truncate()
logfile.seek(0)
# get list of file names
print "Opening File Dialog"
Tk().withdraw()
files = askopenfilenames(title='Select all logs you would like to compile.')
for index in range(len(files)):
print "Loop ", index
print "--- Debug message: Reading a file... ---"
logdata = open((files[index])).readlines()
print "--- Debug message: Finished reading. Writing a file... ---"
# turns logdata into a string and writes it to logfile
logfile.write(''.join(logdata))
logfile.write("\n")
print ""
print "Exited for loop."
logfile.close()
The above code puts the contents of all the files you select into a single text file, but it doesn't sort them.
I was thinking of using regex to search for numbers inside of brackets and then sort each line based on that...?
Here are some sample log files contents.
[xx.xxxxxx] [Text] Text : Text: xxx
[xx.xxxxxx] [Text] Text : Text: xxx
[xx.xxxxxx] [Text] Some text.
There could be multiple lines of text here
These lines could include [brackets.] :(
[xx.xxxxxx] [Text] Text : Text: xxx
The [xx.xxxxxx] is the time stamp in seconds since system startup.