且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用sed仅删除文件中第一个出现的行

更新时间:2023-09-01 19:12:58

您可以使用两个地址的形式:

You could make use of two-address form:

sed '0,/tat/{/tat/d;}' inputfile

这将删除模式的首次出现.

This would delete the first occurrence of the pattern.

info sed报价:

 A line number of `0' can be used in an address specification like
 `0,/REGEXP/' so that `sed' will try to match REGEXP in the first
 input line too.  In other words, `0,/REGEXP/' is similar to
 `1,/REGEXP/', except that if ADDR2 matches the very first line of
 input the `0,/REGEXP/' form will consider it to end the range,
 whereas the `1,/REGEXP/' form will match the beginning of its
 range and hence make the range span up to the _second_ occurrence
 of the regular expression.