How to use sed to replace text with a file path? [duplicate] - linux

This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Closed 2 years ago.
I'm writing a bash script where I need to replace text in a file with a specific file path, but my understanding is that sed does not work with specific characters such as /. I'm wondering if there is some way around this?
Here is my script currently:
currentdir="$PWD"
filepathvar="${currentdir}/settings.ini"
sed -i -e "s/filepath/$filepathvar/g" aimparmstest
When I print out filepathvar everything is as I expect it to be, but it seems the fact that filepathvar contains special characters, it gives me the following error:
sed: -e expression #1, char 13: unknown option to `s'
Is there any way around this? Or perhaps another command I can use? I haven't had any success with changing around the parameters. Any help is greatly appreciated.

You can use any character as the separator (the first character). For example:
echo "a/b/c" | sed -e 's|/|_|g'
In your case:
sed -i -e "s|filepath|$filepathvar|g" aimparmstest

Related

escape string before executing with sed [duplicate]

This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Is it possible to escape regex metacharacters reliably with sed
(4 answers)
Closed 3 months ago.
I have a set of ip addresses declared to a variable IP
IP=["https://127.0.0.1", "https://127.0.0.2", "https://127.0.0.3"]
Need to subtitute the value of IP in some file so i use the sed command
sed -i 's/#hosts: \["https:\/\/localhost:80"\]/hosts: ['$IP']/g' someFile
This errors out with
sed: -e expression #1, char 84: unknown option to `s'
So i tried, which works fine, notice the \ escapes on the IP addresses
sed -i 's/#hosts: \["https:\/\/localhost:80"\]/hosts:["https:\/\/127.0.0.1", "https:\/\/127.0.0.2", "https:\/\/127.0.0.3"]/g'
expected results:
hosts: ["https://127.0.0.1", "https://127.0.0.2", "https://127.0.0.3"]
I can't really influence the value of IP variable because it's gotten from a parameter store with other applications using it. I'm assuming I need to write a function to do the escapes after getting the value from the parameter store? Thanks
The primary issue is you're using sed's default script delimiter of /, which also shows up in the data/strings you're processing, with the net result being that sed can't tell which / are script delimiters vs data.
One solution, as you've figured out, is to escape the / that show up as data.
Another solution is to use a different character (that doesn's show up in the data) as the sed script delimiter.
Addressing a couple other issues with the current code, and using | as the sed script delimiter:
IP='["https://127.0.0.1", "https://127.0.0.2", "https://127.0.0.3"]' # wrap value in single quotes so the whole line is treated as part of the assignment to IP
sed -i "s|#hosts: \[https://localhost:80\]|hosts: $IP|g" someFile # wrap script in double quotes to allow for expansion of $IP
This generates:
$ cat someFile
hosts: ["https://127.0.0.1", "https://127.0.0.2", "https://127.0.0.3"]

How do I escape double quotes in sed? [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 3 years ago.
Trying to figure out how to use a sed command to replace a value when the string i am searching for has double-quotes as part of the string.
I have a config file called ABC.conf that list out the release version like so; "ReleaseSequence": 1555
I want to use sed to change the release number to 2053
I have tried the following sed command;
sed -i 's:"ReleaseSequence":.*:"ReleaseSequence": 2053:' ABC.conf
I get the error message;
sed: -e expression #1, char 24: unknown option to `s'
I have tried to escape the doubel-quotes with [\"], '"""', and "" but sed doesn't like any of those.
The double quotes are not the problem here. You have used : as the separator although your data also contains literal colons. Use a different separator
sed -i 's#"ReleaseSequence":.*#"ReleaseSequence": 2053#' ABC.conf
or backslash-escape the literal colons.

how to use sed command properly to replace values containing / delimiter [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 5 years ago.
File: abc.properties
tomcat.home=/opt/tomcat
Set to /usr/local/tomcat. Following cmd is working.
sed -i "/tomcat.home=/ s/=.*/="usr\\/local\\/tomcat"/" abc.properties
Set to $WORKSPACE/tomcat. Following cmd is NOT working since value of the $WORKSPACE is having / delimeters.
sed -i "/tomcat.home=/ s/=.*/="$WORKSPACE\\/tomcat"/" abc.properties
Anyone has an idea how to success above cmd.
Thank you and appreciate your support...
Sed lets you use any character you want as the delimiter. Whatever follows the s is used as the separator:
sed -Ee 's/foo/bar/'
sed -Ee 's|foo|bar|'
sed -Ee 's#foo#bar#'
^- All of those are equivalent.
The other option is to escape all your / as \/, but that gets nightmarish fast. Prefer to just pick a separator character that doesn't collide with characters you're trying to use for something else.

How to use sed 's/$var/$var2' file [duplicate]

This question already has an answer here:
sed fails with "unknown option to `s'" error [closed]
(1 answer)
Closed 5 years ago.
I'm trying to replace a value shown as a variable with another value shown as a second variable, using the following line:
sed -i "s/$header/$new/" file.f
Where "$header" is the old variable I want replaced with the new one ($new).
I'm getting this error:
sed: -e expression #1, char 20: unknown option to `s'
I've tried
sed -i 's/$header/$new/' file.f
sed -i "s/$header/$new/" file.f
sed -i 's/"$header"/"$new"/' file.f
None of it seem to work.
How should I write this line so I can get the right output (replacing '$header' with '$new' on the file)?
Thanks in advance
sed -i "s/$old/$new/" file
works fine. You can change the separtor if your data has the character /.
sed -i "s#$old#$new#" file
If you are not sure of the content of the variables and to reduce clashing the separator you can use
sed "s^A$old^A$new^A"
to enter the CTRL-A press CTRL-V + CTRL-A (or any other value not expected in vars)

Sed expression with escape sequence [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 6 years ago.
I need to replace the string '../lib' to '/usr/share/server/bootstrap/lib' in a file bootstrap.sh
I used the following sed expression
sed -i -e 's//././/lib///user//share//server//bootstrap//lib/g' bootstrap.sh
It fails with the log
sed: -e expression #1, char 6: unknown option to `s'
Unable to identity the mistake in the expression.Kindly help.
You need to escape every . and /
sed -i -e "s/\.\.\/lib/\/usr\/share\/server\/bootstrap\/lib/" bootstrap.sh
Alternative way to avoid two many backslashes using # as delimiter instead of /.
Thanks sp asic
sed -i "s#../libs#/usr/share/server/bootstrap/lib#" bootstrap.sh

Resources