实现简单的shell sed替换功能
程序员文章站
2022-05-26 10:01:14
...
#脚本文件,用于实现文件里简单的字符串替换
import sys
f = open('yesterday', 'r', encoding='utf-8')
f_new = open('yesterday.bak', 'w', encoding='utf-8')
find_str = sys.argv[1]
replace_str = sys.agrv[2]
for line in f:
if find_str in line:
line = line.replace(find_str, replace_str)
f_new.write(line)
f.close()
f_new.close()