Board logo

标题: [文本处理] 请教sed命令删除不是hello开始的所在行 [打印本页]

作者: netdzb    时间: 2021-1-31 20:23     标题: 请教sed命令删除不是hello开始的所在行

helllo123456
123456hello
gvxshello
hello15487
gdghhfgb
bgdggffhff

删除不是hello开始的所在行,期待运行的结果是
hello123456
hello15487

sed -r -i "/^hello.*/!d" text.txt
我上面这样写,不知道对吗?

如果用editplus,如何写正则完成任务?
作者: Gin_Q    时间: 2021-1-31 20:39

  1. #coding=utf-8
  2. file = 'NEWS.txt'
  3. content = []
  4. par = b'hello'
  5. with open(file, 'rb') as f:
  6.     for n in f:
  7.         if n[:5] == par:
  8.            content.append(n)
  9. with open(file, 'wb') as f:
  10.     f.writelines(content)
复制代码

作者: netdzb    时间: 2021-1-31 20:59

回复 2# Gin_Q

为什么用二进制处理啊。
作者: Gin_Q    时间: 2021-1-31 21:14

回复 3# netdzb


    你可以自己改一下!




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2