悲剧啊,sed替换加了p,所以写了一个脚本来删除。

因需要调整好多文件中涉及异常处理的语句。每个文件中要修改的,和修改后的代码是一样的,所以想到使用sed来替换,使用find加grep -l给sed传参。

sed -i 's/source_str/target_str/g p' `find path -type f |xargs grep -l "source_str"`

结果悲剧了,编译文件报错一看文件target_str竟然有两行,history查看自己的命令,惨了,没办法,写了下面的脚本来删除错误的数据,f为编译报错的记录文件。

import re
import os
rm = '/opt/rm'
f = open(rm)
c = re.compile(r".*?/(\w+\.py).*?\s([0-9]{2})")
newlines = f.read().split(':')
for newline in newlines:
    if newline:
        file = str(re.search(c,newline).group(1))
        line = re.search(c,newline).group(2)
        print file
        print line
        os.system("sed -i '%sd' `find /opt -name %s`" % (line,file))
        print("sed -n '%sd' `find /opt -name %s`" % (line,file))
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值