在bash shell中的Linux字符串中删除一个单词的所有出现 [英] Remove the all occurences of a word from a string in bash shell linux

查看:1431
本文介绍了在bash shell中的Linux字符串中删除一个单词的所有出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串

str="toto1 toto2 toto3 toto4 toto2 toto5"

toto2 在字符串中发生的2倍。如何从字符串中删除所有的 toto2 occurence。

the toto2 is occured 2 times in the string. How to remove all toto2 occurence from the string.

我想这一个:

echo ${str/toto2/}

但是,这将只删除第一次出现 toto2 ,而不是所有的OCCURENCES:

But this will remove only the first occurence of toto2 and not all the occurences:

toto1 toto3 toto4 toto2 toto5 # output

如何删除所有 toto2 OCCURENCES从字符串?

How to remove all toto2 occurences from the string?

推荐答案

发现了它。解决的办法是:

Found it. the solution is:

echo ${str//toto2/}

这篇关于在bash shell中的Linux字符串中删除一个单词的所有出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆