Ciprian Vintea
Ciprian Vintea

Reputation: 468

Remove a word from a string bash

I have the string

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

I want to remove {test} from this string. I used

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

but this returned me

this-is-a-}file

How can I remove } too?

Thanks

Upvotes: 10

Views: 36275

Answers (2)

Shinnok
Shinnok

Reputation: 6409

Also try this bash only oneliner as an alternative:

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

Upvotes: 17

anubhava
anubhava

Reputation: 786091

You can use sed with 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

Upvotes: 11

Related Questions