-1

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.

User892313
  • 225
  • 3
  • 19

1 Answers1

0

Reason on using ampersand operator using stream editor(sed/vim/vi):

$ echo murugesan openssl | sed "s/./& /g;"
m u r u g e s a n   o p e n s s l

Here & refer to the search string/any given pattern. Since I searched . (any character) All characters(using /g where g referring global) all being replaced with same character followed by a space character. Sample output:

$ echo stackoverflow | sed "s/...../& /;s/ ..../ & /;"
stack  over flow

vi delete.txt Press i type any text and few lines. Press Esc and type the following: :%s/./& /g After typing g press Enter Same replaced will happen inside current editor (vim.exe/vi/vim) based on your OS.