linux - How to remove only the first occurrence of a line in a file using sed -


i have following file

titi tata toto tata 

if execute

sed -i "/tat/d" file.txt 

it remove lines containing tat. command returns:

titi toto 

but want remove first line occurs in file containing tat:

titi toto tata 

how can that?

you make use of two-address form:

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

this delete first occurrence of pattern.

quoting info sed:

 line number of `0' can used in address specification  `0,/regexp/' `sed' try match regexp in first  input line too.  in other words, `0,/regexp/' similar  `1,/regexp/', except if addr2 matches first line of  input `0,/regexp/' form consider end range,  whereas `1,/regexp/' form match beginning of  range , hence make range span _second_ occurrence  of regular expression. 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -