实现简单的shell sed替换功能

时间:2021-10-01 17:34:20

通过脚本传参数可以实现替换

# -*-coding:utf-8-*-
# Author:sunhao

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.argv[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()