From 14c28cb2c96baa93cd0a8505ee2b94f0dafd0815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20BERTHO?= Date: Sat, 21 Oct 2017 16:09:06 +0200 Subject: [PATCH] Add output file regex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: RĂ©mi BERTHO --- SSnR.py | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/SSnR.py b/SSnR.py index 6b65030..0e659e4 100755 --- a/SSnR.py +++ b/SSnR.py @@ -37,6 +37,7 @@ def main(): parser.add_argument('-s', '--string', help='String', required=False) parser.add_argument('-i', '--input', help='Input file', required=False, nargs='+') parser.add_argument('-p', '--regex_input', help='Regex input file', required=False) + parser.add_argument('-l', '--regex_output', help='Regex output file', required=False) parser.add_argument('-o', '--output', help='Output file', required=False) parser.add_argument('-r', '--replace', help='Replace', required=False) parser.add_argument('-m', '--print_nb_match', help='Print the number of match in replace', @@ -97,9 +98,23 @@ def main(): return -1 # Get output - if args["output"] is not None: + output_files = [] + if args["regex_output"] is not None: + if input_ex is None: + print("Error: Yu need a regex input file to use a regex output file") + return -1 + for input_filename in filenames: + try: + output_file = open((input_ex.subn(args["regex_output"], input_filename))[0], "w") + output_files.append(output_file) + except OSError as exception: + print("Error: file not found: " + str(exception)) + return -1 + use_output_file = True + elif args["output"] is not None: try: output_file = open(args["output"], "w") + output_files.append(output_file) except OSError as exception: print("Error: file not found: " + str(exception)) return -1 @@ -108,6 +123,7 @@ def main(): use_output_file = False # Search or replace + file_index = 0 if is_file: for filename in filenames: try: @@ -118,21 +134,31 @@ def main(): continue if args["replace"] is not None: - replace_string = replace(ex, string, args["replace"], args["print_nb_match"]) + replace_string, nb_replace = replace(ex, string, args["replace"]) if use_output_file: - output_file.write(replace_string) + output_files[file_index].write(replace_string) + print("File: " + filename) + print(" - Number of replace: " + str(nb_replace)) + if len(output_files) > 1: + file_index += 1 else: print(replace_string) + if args["print_nb_match"]: + print("File: " + filename) + print(" - Number of replace: " + str(nb_replace)) else: print("File: " + filename) search(ex, string, is_file) else: if args["replace"] is not None: - replace_string = replace(ex, string, args["replace"], args["print_nb_match"]) + replace_string, nb_replace = replace(ex, string, args["replace"]) if use_output_file: - output_file.write(replace_string) + print("Number of replace: " + str(nb_replace)) + output_files[0].write(replace_string) else: print(replace_string) + if args["print_nb_match"]: + print("Number of replace: " + str(nb_replace)) else: search(ex, string, is_file) return 0 @@ -172,7 +198,7 @@ def search(ex, string, is_file): print(" - Number of match: " + str(nb_match)) -def replace(ex, string, replace_string, print_nb): +def replace(ex, string, replace_string): """ Replace in a string :param ex: Regular expression @@ -180,9 +206,7 @@ def replace(ex, string, replace_string, print_nb): :param print_nb: Print the number of match """ res = ex.subn(replace_string, string) - if print_nb: - print("Number of match: " + str(res[1])) - return res[0] + return res[0], res[1] def get_line_pos(string): """