Created
April 29, 2020 14:35
-
-
Save yunusemreayhan/9018639b41dea803e07f5174aa4398eb to your computer and use it in GitHub Desktop.
generates diff from given regexp pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
def line_similarity_level(line1, line2): | |
matchlinecnt = 0 | |
for idx in range(len(line1)): | |
idx = idx + 1 | |
char1 = ' ' | |
char2 = ' ' | |
try: | |
char1 = line1[idx] | |
char2 = line2[idx] | |
except: | |
pass | |
if((char1 == char2) or (char1.isdigit() and char2.isdigit())): | |
matchlinecnt = matchlinecnt + 1 | |
return float(matchlinecnt) / len(line1) * 100 | |
def has_line_match_in_lines(line, lines): | |
for templine in lines: | |
foundsimlev = line_similarity_level(templine, line) | |
if foundsimlev > 95: | |
return True | |
return False | |
def get_not_matching_lines(lines1, lines2): | |
not_matching_lines = [] | |
for templine1 in lines1: | |
if(not has_line_match_in_lines(templine1, lines2)): | |
not_matching_lines.append(templine1) | |
return not_matching_lines | |
def get_filtered_version_of_filename(path): | |
filedir = os.path.dirname(path) | |
basename = os.path.basename(path).split(".")[0] | |
extension = os.path.basename(path).split(".")[1] | |
outfilename = os.path.join(filedir, basename + ".filtered." + extension) | |
return outfilename | |
def get_not_matching_version_of_filename(path, path2): | |
filedir = os.path.dirname(path) | |
basename1 = os.path.basename(path).split(".")[0] | |
basename2 = os.path.basename(path2).split(".")[0] | |
extension = os.path.basename(path).split(".")[len(os.path.basename(path).split("."))-1] | |
outfilename = os.path.join(filedir, basename1 + ".not_matching." + basename2 + "." + extension) | |
return outfilename | |
def generate_key_containing_file(path, pattern): | |
lines = get_lines_of_file(path) | |
lines_containing_keyword = [] | |
foundcnt = 0 | |
for line in lines: | |
if re.match(pattern, line, re.IGNORECASE): | |
lines_containing_keyword.append(line) | |
foundcnt = foundcnt + 1 | |
outfile = open(get_filtered_version_of_filename(path), 'w') | |
outfile.writelines(lines_containing_keyword) | |
def get_lines_of_file(path): | |
file = open(path, 'r') | |
return file.readlines() | |
def generate_not_matching_lines_file(filepath1, filepath2): | |
not_matching_lines = get_not_matching_lines(get_lines_of_file(filepath1), get_lines_of_file(filepath2)) | |
outfile = open(get_not_matching_version_of_filename(filepath1, filepath2), 'w') | |
outfile.writelines(not_matching_lines) | |
def process_files(file1, file2, pattern): | |
generate_key_containing_file(file1, pattern) | |
generate_key_containing_file(file2, pattern) | |
generate_not_matching_lines_file(get_filtered_version_of_filename(file1), get_filtered_version_of_filename(file2)) | |
generate_not_matching_lines_file(get_filtered_version_of_filename(file2), get_filtered_version_of_filename(file1)) | |
file1path = 'C:/Users/user/Desktop/fail.log' | |
file2path = 'C:/Users/user/Desktop/success.log' | |
process_files(file1path, file2path, ".*(error|fail).*") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment