Add output file regex
Signed-off-by: Rémi BERTHO <remi.bertho@dalan.fr>
This commit is contained in:
parent
bf0b75dfc1
commit
14c28cb2c9
1 changed files with 33 additions and 9 deletions
42
SSnR.py
42
SSnR.py
|
@ -37,6 +37,7 @@ def main():
|
||||||
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, nargs='+')
|
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('-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('-o', '--output', help='Output file', required=False)
|
||||||
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',
|
||||||
|
@ -97,9 +98,23 @@ def main():
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
# Get output
|
# 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:
|
try:
|
||||||
output_file = open(args["output"], "w")
|
output_file = open(args["output"], "w")
|
||||||
|
output_files.append(output_file)
|
||||||
except OSError as exception:
|
except OSError as exception:
|
||||||
print("Error: file not found: " + str(exception))
|
print("Error: file not found: " + str(exception))
|
||||||
return -1
|
return -1
|
||||||
|
@ -108,6 +123,7 @@ def main():
|
||||||
use_output_file = False
|
use_output_file = False
|
||||||
|
|
||||||
# Search or replace
|
# Search or replace
|
||||||
|
file_index = 0
|
||||||
if is_file:
|
if is_file:
|
||||||
for filename in filenames:
|
for filename in filenames:
|
||||||
try:
|
try:
|
||||||
|
@ -118,21 +134,31 @@ def main():
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if args["replace"] is not None:
|
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:
|
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:
|
else:
|
||||||
print(replace_string)
|
print(replace_string)
|
||||||
|
if args["print_nb_match"]:
|
||||||
|
print("File: " + filename)
|
||||||
|
print(" - Number of replace: " + str(nb_replace))
|
||||||
else:
|
else:
|
||||||
print("File: " + filename)
|
print("File: " + filename)
|
||||||
search(ex, string, is_file)
|
search(ex, string, is_file)
|
||||||
else:
|
else:
|
||||||
if args["replace"] is not None:
|
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:
|
if use_output_file:
|
||||||
output_file.write(replace_string)
|
print("Number of replace: " + str(nb_replace))
|
||||||
|
output_files[0].write(replace_string)
|
||||||
else:
|
else:
|
||||||
print(replace_string)
|
print(replace_string)
|
||||||
|
if args["print_nb_match"]:
|
||||||
|
print("Number of replace: " + str(nb_replace))
|
||||||
else:
|
else:
|
||||||
search(ex, string, is_file)
|
search(ex, string, is_file)
|
||||||
return 0
|
return 0
|
||||||
|
@ -172,7 +198,7 @@ def search(ex, string, is_file):
|
||||||
print(" - Number of match: " + str(nb_match))
|
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
|
Replace in a string
|
||||||
:param ex: Regular expression
|
:param ex: Regular expression
|
||||||
|
@ -180,9 +206,7 @@ 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)
|
||||||
if print_nb:
|
return res[0], res[1]
|
||||||
print("Number of match: " + str(res[1]))
|
|
||||||
return res[0]
|
|
||||||
|
|
||||||
def get_line_pos(string):
|
def get_line_pos(string):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue