yoano
yoano

Reputation: 1546

Bash replace string which contains single quotes with other string

I am trying to replace a string with sed. Unfortunately the string contains single quotes and "::" symbols. Therefore I have some troubles using sed.
I need to change configuration::set('feature', '0') into configuration::set('feature', '1').
What I tried:

sed 's%configuration::set('feature', '0')%configuration::set('feature', '1')%g'  file.cf

Upvotes: 0

Views: 118

Answers (2)

Tom Fenech
Tom Fenech

Reputation: 74596

Use double quotes around the whole thing so that ' doesn't end the command string and use a capture group to avoid repetition:

sed "s%\\(configuration::set('feature', '\\)0'%\\11'%g"  file.cf

Based on the comments (thanks), I've added the closing quote to the match and replacement to make it slightly more robust. I opted to go against my own advice here due to the number of characters being so low.

Upvotes: 1

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89547

It's only a quote problem. Enclose your sed command in double quotes (if there is nothing that bash may expand) or escape all single quotes inside.

sed 's/configuration::set('"'"'feature'"'"', '"'"'0'"'"')/configuration::set('"'"'feature'"'"', '"'"'1'"'"')/g'

Upvotes: 1

Related Questions