python - Pythonic script that ignores timestamps in log files -
there 2 log files : log a
, log b
.
log 2015-07-12 08:50:33,904 [collection-3]info app -executing scheduled job: system: choppa1 2015-07-12 09:56:45,060 [collection-3] info app - executing scheduled job: system: choppa1 2015-07-12 10:00:00,001 [analytics_worker-1] info app - trigger job anbuildauthorizationjob fired. 2015-07-12 11:00:00,007 [analytics_worker-1] info app - starting anbuildauthorizationjob job. log b 2014-07-12 09:50:33,904 [collection-3] info app - executing scheduled job: system: choppa1 2014-07-12 09:56:45,060 [collection-3] info app - executing scheduled job: system: choppa1 2014-07-12 10:00:00,001 [analytics_worker-1] info app - trigger job anbuildauthorizationjob fired. 2014-07-12 10:00:00,007 [analytics_worker-1] info app - starting anbuildauthorizationjob job.
the 2 log files have same content timestamp different. need compare 2 files ignoring timestamp i.e. compare each line of both files , though have different timestamp, shouldn't report difference. wrote following python script this:
#!/usr/bin/python import re import difflib program = open("log1.txt", "r") program_contents = program.readlines() program.close() new_contents = [] pat = re.compile("^[^0-9]") line in program_contents: if re.search(pat, line): new_contents.append(line) program = open("log2.txt", "r") program_contents1 = program.readlines() program.close() new_contents1 = [] pat = re.compile("^[^0-9]") line in program_contents1: if re.search(pat, line): new_contents1.append(line) diff=difflib.ndiff(new_contents,new_contents1) print(''.join(diff))
is there more efficient way of writing above script?? , above script works if timestamp in beginning of line. want write python script should work if timestamp somewhere in middle of line. can please me how this?
i change pat = re.compile("^[^0-9]") pat = re.compile("\d{4}-d{2}-d{2}
and better open files
open(filename) f:
this way python close file you, no need close(f) statement.
Comments
Post a Comment