Print filename only if one found
Signed-off-by: Rémi BERTHO <remi.bertho@dalan.fr>
This commit is contained in:
parent
91fbfe0c00
commit
0ba4b373cb
1 changed files with 14 additions and 10 deletions
24
SSnR.py
24
SSnR.py
|
@ -105,7 +105,8 @@ def main():
|
||||||
return -1
|
return -1
|
||||||
for input_filename in filenames:
|
for input_filename in filenames:
|
||||||
try:
|
try:
|
||||||
output_file = open((input_ex.subn(args["regex_output"], input_filename))[0], "w", encoding="utf8")
|
output_file = open((input_ex.subn(args["regex_output"], input_filename))[0], "w",
|
||||||
|
encoding="utf8")
|
||||||
output_files.append(output_file)
|
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))
|
||||||
|
@ -150,8 +151,11 @@ def main():
|
||||||
print("File: " + filename)
|
print("File: " + filename)
|
||||||
print(" - Number of replace: " + str(nb_replace))
|
print(" - Number of replace: " + str(nb_replace))
|
||||||
else:
|
else:
|
||||||
print("File: " + filename)
|
nb_match, str_found = search(ex, string, is_file)
|
||||||
search(ex, string, is_file)
|
if nb_match > 0:
|
||||||
|
print("File: " + filename)
|
||||||
|
print(" - Number of match: " + str(nb_match))
|
||||||
|
print(str_found)
|
||||||
else:
|
else:
|
||||||
if args["replace"] is not None:
|
if args["replace"] is not None:
|
||||||
replace_string, nb_replace = replace(ex, string, args["replace"])
|
replace_string, nb_replace = replace(ex, string, args["replace"])
|
||||||
|
@ -163,7 +167,9 @@ def main():
|
||||||
if args["print_nb_match"]:
|
if args["print_nb_match"]:
|
||||||
print("Number of replace: " + str(nb_replace))
|
print("Number of replace: " + str(nb_replace))
|
||||||
else:
|
else:
|
||||||
search(ex, string, is_file)
|
nb_match, str_found = search(ex, string, is_file)
|
||||||
|
print("Number of match: " + str(nb_match))
|
||||||
|
print(str_found)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def compile_regex(ex, ignore_case):
|
def compile_regex(ex, ignore_case):
|
||||||
|
@ -192,16 +198,15 @@ def search(ex, string, is_file):
|
||||||
|
|
||||||
ite = ex.finditer(string)
|
ite = ex.finditer(string)
|
||||||
nb_match = 0
|
nb_match = 0
|
||||||
|
str_found = ""
|
||||||
for match in ite:
|
for match in ite:
|
||||||
nb_match += 1
|
nb_match += 1
|
||||||
if is_file:
|
if is_file:
|
||||||
num_line, begin_pos, end_pos = find_line(match.start(0), match.end(0), new_lines)
|
num_line, begin_pos, end_pos = find_line(match.start(0), match.end(0), new_lines)
|
||||||
print(" - Found \"" + match.group(0) + "\" at line " + str(num_line) + " [" + str(begin_pos) +
|
str_found += " - Found \"%s\" at line %d [%d:%d]\n" % (match.group(0), num_line, begin_pos, end_pos)
|
||||||
":" + str(end_pos) + "]")
|
|
||||||
else:
|
else:
|
||||||
print(" - Found \"" + match.group(0) + "\" at [" + str(match.start(0) + 1) +
|
str_found += " - Found \"%s\" at [%d:%d]\n" % (match.group(0), match.start(0), match.end(0))
|
||||||
":" + str(match.end(0)) + "]")
|
return nb_match, str_found
|
||||||
print(" - Number of match: " + str(nb_match))
|
|
||||||
|
|
||||||
|
|
||||||
def replace(ex, string, replace_string):
|
def replace(ex, string, replace_string):
|
||||||
|
@ -244,4 +249,3 @@ def find_line(begin_pos, end_pos, new_lines):
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue