Add output file

Signed-off-by: Rémi BERTHO <remi.bertho@dalan.fr>
This commit is contained in:
Rémi BERTHO 2017-10-16 21:42:43 +02:00
parent 65dd8bf97d
commit 19e9e23f7e
Signed by: dalan
GPG key ID: EE3B917931C07B64

12
SSnR.py Normal file → Executable file
View file

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# SSnR.py # SSnR.py
@ -34,6 +34,8 @@ def main():
parser.add_argument('-e', '--regex', help='Regex', required=True) parser.add_argument('-e', '--regex', help='Regex', required=True)
parser.add_argument('-s', '--string', help='String', required=False) parser.add_argument('-s', '--string', help='String', required=False)
parser.add_argument('-i', '--input', help='Input file', required=False, type=open) 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('-r', '--replace', help='Replace', required=False)
parser.add_argument('-m', '--print_nb_match', help='Print the number of match in replace', parser.add_argument('-m', '--print_nb_match', help='Print the number of match in replace',
required=False, action='store_true') required=False, action='store_true')
@ -68,7 +70,11 @@ def main():
# Search or replace # Search or replace
if args["replace"] is not None: 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: else:
search(ex, string, is_file) search(ex, string, is_file)
return 0 return 0
@ -116,9 +122,9 @@ def replace(ex, string, replace_string, print_nb):
:param print_nb: Print the number of match :param print_nb: Print the number of match
""" """
res = ex.subn(replace_string, string) res = ex.subn(replace_string, string)
print(res[0])
if print_nb: if print_nb:
print("Number of match: " + str(res[1])) print("Number of match: " + str(res[1]))
return res[0]
def get_line_pos(string): def get_line_pos(string):
""" """