Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace only the first occurence matching a regex with sed

Tags:

regex

sed

I have a string

test:growTest:ret

And with sed i would to delete only test: to get :

growTest:ret

I tried with

sed '0,/RE/s/^.*://'

But it only gives me

ret

Any ideas ?

Thanks

like image 859
Unitech Avatar asked Oct 25 '25 06:10

Unitech


1 Answers

Modify your regexp ^.*: to ^[^:]*:

All you need is that the .* construction won't consume your delimiter — the colon. To do this, replace matching-any-char . with negated brackets: [^abc], that match any char except specified.

Also, don't confuse the two circumflexes ^, as they have different meanings: first one matches beginning of string, second one means negated brackets.

like image 71
ulidtko Avatar answered Oct 27 '25 19:10

ulidtko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!