Add output file regex

Signed-off-by: Rémi BERTHO <remi.bertho@dalan.fr>
This commit is contained in:
Rémi BERTHO 2017-10-21 16:09:06 +02:00
parent bf0b75dfc1
commit 14c28cb2c9
Signed by: dalan
GPG key ID: EE3B917931C07B64

42
SSnR.py
View file

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