python 调用sed进行文件 替换/添加/删除

#!/usr/bin/python3
import os
import sys
 
result = os.popen('find . -name "*.v"')## not  os.system
res    = result.read()
print(res)
res_split = res.splitlines() ## split found files list with '\n'
for line in res_split :
    print("%s\n" % (line))
    os.system(r'sed -i "/abcd/s/\/0/g" %s' % (line))   ## find line include abcd and replace 'infinity' with '0' in this line
    os.system(r'sed -i "s/\/0/g" %s' % (line))   ## replace
    os.system(r'sed -i "/\/i\add_a_line_before" %s' % (line))   ## add a line before
    os.system(r'sed -i "/\/a\add_a_line_after" %s' % (line))   ## add a line after
    os.system(r'sed -i "/\/d" %s' % (line))   ## del a line include infinity

以上代码包含

替换匹配行字符串

替换所有匹配字符串

在匹配行前或行后添加一行

删除匹配行

你可能感兴趣的:(编程语言和脚本)