sed linux replace complex link
I am having this challenge to pass a link from a variable:
InstallLocation=/opt/software/software.properties
ReplaceeVar=http://localhost:1234/ab/cd/{company}/{employee}/
sed -i "s/Change_This_Url/"${ReplaceeVar}"/g" "$InstallLocation"
Built url would fail like:
sed: -e expression #1, char xx: unterminated `s' command
First, I don't know what all your quotes are for. Just put one set of quotes around the substitution command.
You use slashes to delimit regular expression and replacement string in the sed
command. ReplaceeVar
contains lots of slashes as well. The resulting command is
sed -i "s/Change_This_Url/http://localhost:1234/ab/cd/{company}/{employee}//g" "$InstallLocation"
Do you see why this doesn't work?
The solution is to not use slashes, but another character for delimiting regex and replacement string. Pipe signs are even prettier than slashes, in my opinion:
sed -i "s|Change_This_Url|${ReplaceeVar}|g" "$InstallLocation"