From 19e9e23f7e12fa9d0b8f371d4c1ee6253328d716 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20BERTHO?= Date: Mon, 16 Oct 2017 21:42:43 +0200 Subject: [PATCH] Add output file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: RĂ©mi BERTHO --- SSnR.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) mode change 100644 => 100755 SSnR.py 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): """