Replace only the first occurrence matching the regular expression with sed

I have a line

test:growTest:ret

And with sed, I would only remove the test: get:

growTest:ret

I tried using

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

But it only gives me

ret

Any ideas?

thank

+3
source share
3 answers

If I understand your question, you want strings such as test:growTest:retbecome growTest:ret.

You can use:

sed -i 's/test:(.*$)/\1/'

i means in-place editing.

s / one / two / replaces the occurrences of one with two.

Thus, it replaces "test: (. * $)" With "\ 1". Where \ 1 is the contents of the first group, which corresponds to the regular expression inside curly braces.

"test: (. * $)" "test:", . sed.

+1

^.*: ^[^:]*:

, , , .* - . match-any- char . : [^abc], char, .

, circumflexes ^, : , - .

+5
  • Sed . , ^.*: test:growTest:, test:.
  • , sed . .
+1

Source: https://habr.com/ru/post/1788583/


All Articles