-1

I have file like this,then now I want to delete only the first def_xxx

abc_xxx
def_xxx
ghi_xxx
abc_yyy
def_yyy
ghi_yyy

It delete the two lines def_xxx,def_yyy.

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

How can I delete the only first line def_xxx??

Inian
  • 80,270
  • 14
  • 142
  • 161
whitebear
  • 11,200
  • 24
  • 114
  • 237

1 Answers1

3
sed -e '0,/def/{/def/d;}' myfile.txt

This deletes the first occurrence of the pattern.

From its manual:

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.

Ref: https://linux.die.net/man/1/sed

Panwen Wang
  • 3,573
  • 1
  • 18
  • 39
  • 1
    May as well provide the full `def_xxx` in case there is actual a larger file with other `def` prefixed lines -- but good answer. – David C. Rankin Jul 14 '20 at 04:07