Compare commits
2 commits
65dd8bf97d
...
25d6729e03
Author | SHA1 | Date | |
---|---|---|---|
25d6729e03 | |||
19e9e23f7e |
1 changed files with 28 additions and 10 deletions
38
SSnR.py
Normal file → Executable file
38
SSnR.py
Normal file → Executable file
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
#
|
#
|
||||||
# SSnR.py
|
# SSnR.py
|
||||||
|
@ -33,21 +33,24 @@ def main():
|
||||||
parser = argparse.ArgumentParser(description='Search and replace tool', prog='SSnR')
|
parser = argparse.ArgumentParser(description='Search and replace tool', prog='SSnR')
|
||||||
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)
|
||||||
|
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',
|
||||||
required=False, action='store_true')
|
required=False, action='store_true')
|
||||||
parser.add_argument('-c', '--ignore_case', help='Ignore the case',
|
parser.add_argument('-c', '--ignore_case', help='Ignore the case',
|
||||||
required=False, action='store_true')
|
required=False, action='store_true')
|
||||||
try:
|
|
||||||
args = vars(parser.parse_args())
|
args = vars(parser.parse_args())
|
||||||
except FileNotFoundError as exception:
|
|
||||||
print("Error: file not found: " + str(exception))
|
|
||||||
return -1
|
|
||||||
|
|
||||||
# Get input
|
# Get input
|
||||||
if args["input"] is not None:
|
if args["input"] is not None:
|
||||||
string = args["input"].read()
|
try:
|
||||||
|
with open(args["input"], "r") as file:
|
||||||
|
string = file.read()
|
||||||
|
except OSError as exception:
|
||||||
|
print("Error: file not found: " + str(exception))
|
||||||
|
return -1
|
||||||
is_file = True
|
is_file = True
|
||||||
elif args["string"] is not None:
|
elif args["string"] is not None:
|
||||||
string = args["string"]
|
string = args["string"]
|
||||||
|
@ -56,6 +59,17 @@ def main():
|
||||||
print("Error: You need an input string or file")
|
print("Error: You need an input string or file")
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
# Get output
|
||||||
|
if args["output"] is not None:
|
||||||
|
try:
|
||||||
|
output_file = open(args["output"], "w")
|
||||||
|
except OSError as exception:
|
||||||
|
print("Error: file not found: " + str(exception))
|
||||||
|
return -1
|
||||||
|
use_output_file = True
|
||||||
|
else:
|
||||||
|
use_output_file = False
|
||||||
|
|
||||||
# Compile regex
|
# Compile regex
|
||||||
try:
|
try:
|
||||||
ex = compile_regex(args["regex"])
|
ex = compile_regex(args["regex"])
|
||||||
|
@ -68,7 +82,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 use_output_file:
|
||||||
|
output_file.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 +134,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):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue