Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all occurrences of a word from a string

Tags:

string

bash

shell

I have the following string

str="toto1 toto2 toto3 toto4 toto2 toto5"

the toto2 occurs twice in the string. How can I remove all toto2 occurrences from the string?

I tried this:

echo ${str/toto2/}

But this will remove only the first occurrence of toto2.

toto1 toto3 toto4 toto2 toto5 # output

How can I remove all toto2 occurrences from the string?

like image 699
MOHAMED Avatar asked Dec 26 '13 10:12

MOHAMED


1 Answers

Found it. The solution is:

echo "${str//toto2/}"
like image 186
MOHAMED Avatar answered Oct 17 '22 17:10

MOHAMED