0

I have the below string in a file filename="mongo-template.txt".

db.EndpointUrls.insert({"BACKEND_APP" : "DCC", "APPLICATION_NAME" : "dcc-core-api", "COUNTRY_CODE" : "IN", "ENDPOINT_URL" : "SERVICE-ENDPOINT");

I need to replace SERVICE-ENDPOINT with https://test-in.mydomain.com/api/users

filename="mongo-template.txt"

serviceendpoint="https://test-in.mydomain.com/api/users"
$sed "s/SERVICE-ENDPOINT/$serviceendpoint/g" "$filename"

I am getting the below error.

sed: -e expression #1, char 27: unknown option to `s'.
  • If the serviceendpoint contains / character, I am getting the error.
  • If the serviceendpoint does not contain / character, it replaces the string in the file.

The below link helped me to resolve my issue.

How to pass a variable containing slashes to sed

user1346346
  • 195
  • 2
  • 16

1 Answers1

0

The standard solution is to use a different delimiter. But you can also use perl:

$ repl=this/string/has/slashes
$ echo foo | sed "s|foo|$repl|g"
this/string/has/slashes
$ echo foo | r="$repl" perl -pe 's/foo/$ENV{r}/g'
this/string/has/slashes
$ echo foo | r="$repl" perl -MEnv -pE 's/foo/$r/g'
this/string/has/slashes
William Pursell
  • 204,365
  • 48
  • 270
  • 300