Ineedhelp
Ineedhelp

Reputation: 1

Bash replace content between two delimiters

I have this string:

test=test1&test2=test3&test3

My question is: how can I replace all words between = and & with replaced on Bash?

So I can get an output like this:

test=replaced&test2=replaced&test3

Thanks in advance.

Upvotes: 0

Views: 1013

Answers (2)

dash-o
dash-o

Reputation: 14468

You can use bash "extended glob" for pure bash solution, using the substitue (${var//} operator.

X='test=replaced&test2=replaced&test3'
shopt -s extglob
echo ${X//=*([!&])&/=replaced&}

Upvotes: 1

choroba
choroba

Reputation: 241998

Use sed. Search for a string starting with a = and continuing with non-ampersands, replace it with =replaced.

echo 'test=test1&test2=test3&test3' | sed 's/=[^&]*/=replaced/g'

The /g means globally, without it, only the first occurrence would have been replaced.

Upvotes: 0

Related Questions