Replacing whole string starting with specific works

Hi guys,

So what I am trying to accomplish is to replace a whole string starting with some designated string.

eg:

When even I find a string starting with :

eai.endpoint.url=

replace the entire line with:

eai.endpoint.url=http://www.endpoint.com/API

Righ now I am trying to accomplish this using sed and regex.

BASE_FILE=/opt/sh.properties
CAC_ENDPOINT="eai.endpoint.url=*"
NEW_ENDPOINT="eai.endpoint.url=http://www.endpoint.com/API"


sed -i "s/${CAC_ENDPOINT}/${NEW_ENDPOINT}/g" $BASE_FILE

But seems like regex is not working and I am getting:

sed: -e expression #1, char 45: unknown option to `s'

For me :

eai.endpoint.url=*

means find all lines starting with eai.endpoint.url=

Am I doing this wrong ?

Hi, the biggest problem is with the slashes that are part of the variables. Try using | as a delimiter instead..:

sed "\|${CAC_ENDPOINT}|s|.*|${NEW_ENDPOINT}|"
1 Like

${NEW_ENDPOINT} has / inserted in it and when it gets expanded sed sees
s/eai.endpoint.url=*/eai.endpoint.url=http://www.endpoint.com/API/g
The slashes in the URI gets confused with the delimiters.
Escaping them in the variable assignment might work as intended.

NEW_ENDPOINT='eai.endpoint.url=http:\/\/www.endpoint.com\/API'

There is a subtle issue as well.

CAC_ENDPOINT="eai.endpoint.url=*"

The * is a pattern matching for the shell and it gets interpreted by it. This variable might end up having assigned the name of every file in the directory that would match that. Furthermore, if it makes it as just a string, when it gets converted as a regex in sed , it only means match the following chars: eai followed by any char except the newline followed by endpoint (these are individual chars), followed by any character except the newline, followed by url , followed by one or many = or none.

1 Like

As it is your regex means something different: First, "." means "any single character", and "*" means "the preceding expression or single character is optional". Your regex therefore means:

"eai", followed by any single character, followed by the fixed string "endpoint", followed by any single character, followed by "url" and optionally a "=" at the end. This string, for instance: "eaiXendpointYurl" would match your regex.

This here would do what you want (note the difference between "." - any character - and "\." - a literal dot. Further, as you said the string you search for is at the beginning of the line, i added a "^" as anchor. It symbolizes the beginning of line):

^eai\.endpoint\.url=.*

It is probably easier to write your replacement like this (you won't need the final ".*"):

sed '/^eai\.endpoint\.url=/ s|.*|eai.endpoint.url=http://www.endpoint.com/API|'

or, using the variable names you used - note the quoting:

CAC_ENDPOINT='^eai\.endpoint\.url='
NEW_ENDPOINT='eai.endpoint.url=http://www.endpoint.com/API'

sed '/'"{CAC_ENDPOINT}"'/ s|.*|'"${NEW_ENDPOINT}"'|'

I hope this helps.

bakunin

1 Like

Thanks guys for the answers and their explanations. Worked perfectly !