-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
python实现sed #96
Comments
import argparse
import re
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--expr", help="expression")
parser.add_argument("filename", type=str)
return parser.parse_args()
def match_file(old_expr, new_expr, file, cout):
with open(file, 'r', encoding='utf-8') as f:
for line in f.readlines():
result = re.sub(old_expr, new_expr, line, count=cout)
print(result, end='')
def read_file(filename1, filename2):
with open(filename1, 'r', encoding='utf-8') as f1:
for line1 in f1:
print(line1.strip())
with open(filename2, 'r', encoding='utf-8') as f2:
for line2 in f2:
print(line2.strip())
def sed_func(args):
if args.expr.split('/')[0] == 's' and \
args.expr.split('/')[-1] == 'g':
match_file(args.expr.split('/')[1], args.expr.split('/')[2],
args.filename, cout=0)
elif args.expr.split('/')[0] == 's' and \
args.expr.split('/')[-1] != 'g':
match_file(args.expr.split('/')[1], args.expr.split('/')[2],
args.filename, cout=1)
elif args.expr.split()[0] == 'r':
read_file(args.filename, args.expr.split()[1])
args = parse_args()
sed_func(args)
|
最后一个我觉得没啥用? |
测了下sed
|
我自己实现的带-i的版本 #!/usr/bin/env python
# -*- encoding: utf-8 -*-
import argparse
import re
### usage:
# python sed.py -e "s/5月 [0-9][0-9]/DATE/g" 123.txt
# python sed.py -e "s/5月 [0-9][0-9]/DATE/" 123.txt
# python sed.py -e "s/admin/moon/g" 123.txt
# python sed.py -e "s/admin/moon/" 123.txt
# python sed.py -e "s/admin/moon/g" -i 123.txt
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--expr", help="expression")
parser.add_argument("-i", "--inplace", action="store_true", default=False, help="edit files in place")
parser.add_argument("filename", type=str)
return parser.parse_args()
def write_file(filename, contents):
if not contents:
return
with open(filename, 'w+', encoding='utf-8') as f:
f.writelines(contents)
def match_file(old_expr, new_expr, file, count):
inplace = args.inplace
contents = []
with open(file, 'r', encoding='utf-8') as f:
for line in f.readlines():
result = re.sub(old_expr, new_expr, line, count=count)
if inplace:
contents.append(result)
else:
print(result, end='')
if inplace:
write_file(args.filename, contents)
def sed_func(args):
if args.expr.split('/')[0] == 's' and args.expr.split('/')[-1] == 'g':
match_file(args.expr.split('/')[1], args.expr.split('/')[2], args.filename, count=0)
elif args.expr.split('/')[0] == 's' and args.expr.split('/')[-1] != 'g':
match_file(args.expr.split('/')[1], args.expr.split('/')[2], args.filename, count=1)
if __name__ == "__main__":
args = parse_args()
sed_func(args)
|
python sed.py -e "s/^CERT_EXPIRE_DAYS=.*/CERT_EXPIRE_DAYS=3650/" -i 123.txt |
最新,调整match_file的参数顺序
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
初稿来自
https://blog.csdn.net/zhulinxiaoan/article/details/106344821
The text was updated successfully, but these errors were encountered: