2017-10-06 52 views
0

我有这样一个变量:(在新行中的每个字)在bash变量删除所有文字occurence

> echo $LIST 
toto 
toto2 
titi 
rererer 
dfs 
sdfsdf 
titi 
titi 

我尝试删除的“蒂蒂”所有出现,以获得:

> echo $LIST 
toto 
toto2 
rererer 
dfs 
sdfsdf 

我试图与LIST = $(回声$ {// LIST蒂蒂/})并除去它,但它也删除新行,并得到这样的结果:

> echo $LIST 
toto toto2 rererer dfs sdfsdf 

我的问题是如何删除所有出现保持每个单词在一行吗? 感谢提前:)

+0

子shell和回声是多余的 – 123

+0

你可以尝试:echo“$ list”| grep -v titi –

回答

2

你需要把周围"${LIST//titi/}"引号,否则空白将被折叠:

 
$ LIST='toto 
> toto2 
> titi 
> rererer 
> dfs 
> sdfsdf 
> titi 
> titi' 
$ echo "${LIST//titi/}" 
toto 
toto2 

rererer 
dfs 
sdfsdf 


但你也可以只直接分配:

LIST=${LIST//titi/} 
echo "$LIST" # quotes are important here!