diff --git a/SSnR.py b/SSnR.py old mode 100644 new mode 100755 index d45e6b9..5d47f65 --- a/SSnR.py +++ b/SSnR.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # # SSnR.py @@ -34,6 +34,8 @@ def main(): parser.add_argument('-e', '--regex', help='Regex', required=True) parser.add_argument('-s', '--string', help='String', required=False) parser.add_argument('-i', '--input', help='Input file', required=False, type=open) + parser.add_argument('-o', '--output', help='Output file', required=False, + type=argparse.FileType('w')) parser.add_argument('-r', '--replace', help='Replace', required=False) parser.add_argument('-m', '--print_nb_match', help='Print the number of match in replace', required=False, action='store_true') @@ -68,7 +70,11 @@ def main(): # Search or replace if args["replace"] is not None: - replace(ex, string, args["replace"], args["print_nb_match"]) + replace_string = replace(ex, string, args["replace"], args["print_nb_match"]) + if args["output"] is not None: + args["output"].write(replace_string) + else: + print(replace_string) else: search(ex, string, is_file) return 0 @@ -116,9 +122,9 @@ def replace(ex, string, replace_string, print_nb): :param print_nb: Print the number of match """ res = ex.subn(replace_string, string) - print(res[0]) if print_nb: print("Number of match: " + str(res[1])) + return res[0] def get_line_pos(string): """