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
1 changed files with 9 additions and 3 deletions

12
SSnR.py Normal file → Executable file
View File

@ -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):
"""