I am trying to use sed to replace a line of text in a configuration file.
Original text:
%prod.db=${DATABASE_URL}?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory"
Desired text:
%prod.db=${DATABASE_URL}?ssl=true&sslmode=require&sslfactory=org.postgresql.ssl.NonValidatingFactory"
I tried the following:
old="%prod.db=\${DATABASE_URL}?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory"
new="%prod.db=\${DATABASE_URL}?ssl=true&sslmode=require&sslfactory=org.postgresql.ssl.NonValidatingFactory"
sed 's/^'"$old"'.*/'"$new"'/g' filename
And somehow ended up with this result:
%prod.db=${DATABASE_URL}?ssl=true%prod.db=${DATABASE_URL}?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactorysslmode=require%prod.db=${DATABASE_URL}?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactorysslfactory=org.postgresql.ssl.NonValidatingFactory
I assume the dollar sign is causing the issue and that it needs to be escaped somehow, but i have tried multiple ways to escape the replacement text without success, for example this answer: Escape a string for a sed replace pattern
I would be really grateful if anyone could help me solve this issue.