Remove word from bash string

I have a line

file="this-is-a-{test}file" 

      

I want to remove {test}

from this line. I used

echo $file | sed 's/[{][^}]*//'  

      

but it brought me back

this-is-a-}file

      

How can I delete }

too?

thank

+3


source to share


2 answers


You can use sed

with the correct regex:

s="this-is-a-{test}file"
sed 's/{[^}]*}//' <<< "$s"
this-is-a-file

      



Or this awk:

awk -F '{[^}]*}' '{print $1 $2}' <<< "$s"
this-is-a-file

      

+4


source


Also try using bash only oneliner as an alternative:



s="this-is-a-{test}file"
echo ${s/\{test\}/}

      

+5


source







All Articles