简体   繁体   English

删除bash变量中所有出现的单词

[英]removing all word occurence in a bash variable

i have a variable like this: (each word in a new line) 我有一个像这样的变量:(每行中的每个单词)

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

I try to remove all occurences of "titi" to obtain that: 我尝试删除所有出现的“ titi”来获得:

> echo $LIST
toto
toto2
rererer
dfs
sdfsdf

i tried with LIST=$(echo ${LIST//titi/}) and it remove it but it also delete new line and give this result: 我尝试使用LIST = $(echo $ {LIST // titi /})并将其删除,但同时也删除了新行并给出以下结果:

> echo $LIST
toto toto2 rererer dfs sdfsdf

My question is how to remove all occurences keeping each word in a line ? 我的问题是如何删除所有出现的单词,使每个单词排成一行? Thanks in advance :) 提前致谢 :)

You need to put quotes around "${LIST//titi/}" , otherwise whitespace will be collapsed: 您需要在"${LIST//titi/}"加上引号,否则空格将被折叠:

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

rererer
dfs
sdfsdf

But you can also just assign directly: 但是您也可以直接分配:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM