【问题标题】:How to delete the first line of match by sed [duplicate]如何通过sed删除匹配的第一行[重复]
【发布时间】:2020-07-14 14:04:02
【问题描述】:

我有这样的文件,那么现在我只想删除第一个def_xxx

abc_xxx
def_xxx
ghi_xxx
abc_yyy
def_yyy
ghi_yyy

删除def_xxx,def_yyy这两行。

sed -e '/def/d' myfile.txt

如何删除唯一的第一行def_xxx??

【问题讨论】:

  • 使用完整模式。如果你在def_xxx 之前有一个def_www,那么简单地使用def 将会失败。

标签: sed


【解决方案1】:
sed -e '0,/def/{/def/d;}' myfile.txt

这会删除模式的第一次出现。

来自其手册:

0,addr2
Start out in "matched first address" state, until addr2 is found. This is similar
to 1,addr2, except that if addr2 matches the very first line of input the 
0,addr2 form will be at the end of its range, whereas the 1,addr2 form will 
still be at the beginning of its range. This works only when addr2 is a 
regular expression.

参考:https://linux.die.net/man/1/sed

【讨论】:

  • 也可以提供完整的def_xxx,以防实际存在带有其他def 前缀行的更大文件——但很好的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-07
  • 2019-01-13
  • 2017-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多