import sys import re import argparse import os ap = argparse.ArgumentParser() ap.add_argument('-r', '--regex', required=False, default=r'^(\s|\t)*(INSERT|DELETE|UPDATE|SELECT)(\s|\t)') ap.add_argument('-e', '--ext', required=False, default='oneline') ap.add_argument('-l', '--fromlog', required=False, default=False, action='store_true') args, files = ap.parse_known_args() assert files, "Kein File angegeben" assert all(map(os.path.isfile, files)), "Eines der Files gibt es nicht" regstr = args.regex if args.fromlog and '-r' not in sys.argv: # We begin wit a date in a usual sql log regstr = r'^(\t|\s)*[0-9]{2,4}[\-./][0-9]{2}[\-./][0-9]{2,4}' newline_reg = re.compile(regstr, re.IGNORECASE) for file in files: lastline = '' _name, _ext = os.path.splitext(file) oneline_file = f"{_name}.{args.ext}{_ext}" with open(file) as rf, open(oneline_file, 'w') as wf: for line in rf: line = line.strip() if newline_reg.search(line): if lastline and not lastline.endswith(';'): wf.write(";") wf.write("\n") wf.write(line + ' ') lastline = line print(f"{file} onelined to {oneline_file}")