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)
Related
This question already has an answer here:
sed fails with "unknown option to `s'" error [closed]
(1 answer)
Closed 1 year ago.
I'm trying to replace some text in a file as described in title. What i tried:
newdir=/dir/to/my/file
sed -i "s/'MyDir'/${newdir}/g" myconf.conf
The command above gives this error:
unknown option to `s'
The problem is that $newdir contains / characters, so your sed command ends up looking like s/'MyDir'//dir/to/my/file/g, which won't work -- the first / effectively terminates your sed expression, and everything else is garbage.
Fortunately, sed let's you use any character as a delimiter to the s command, so you could write instead:
sed -i "s|'MyDir'|${newdir}|g" myconf.conf
One way to get around the "does my data contain the delimiter" problem is to use shell variable expansion to escape the delimiter character:
sed -i "s|'MyDir'|${newdir//|/\\|}|g" myconf.conf
Demo:
$ newdir="/a/dir/with|a/pipe"
$ sed "s|'MyDir'|${newdir}|g" <<< "this is 'MyDir' here"
sed: 1: "s|'MyDir'|/a/dir/with|a ...": bad flag in substitute command: 'a'
$ sed "s|'MyDir'|${newdir//|/\\|}|g" <<< "this is 'MyDir' here"
this is /a/dir/with|a/pipe here
You can do this with the default slash delimiter, with more escapes
sed -i "s/'MyDir'/${newdir//\//\\\/}/g" myconf.conf
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
This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed last year.
I have a small script that does some global string replacements in various files. This should be done with sedso that only the affected lines are changed.
Preparation:
#!/bin/sh
find="Mücke Motorsport"
replace="Mücke Motorsport Racing Team"
file="/Users/meyer/Dropbox/Dev/App Framework iOS/dev/myfile.txt"
This is my problematic line in the script:
sed -i s/"$find"/"$replace"/g "$file"
Does not work (sed: 1: "/Users/meyer/Dropb ...": invalid command code o). Next try:
sed -i 's/'"$find"'/'"$replace"'/g' "$file"
Does not work (sed: 1: "/Users/meyer/Dropb ...": invalid command code o). Next try:
sed -i "s/$find/$replace/g" "$file"
Does not work (sed: 1: "/Users/meyer/Dropb ...": invalid command code o). Next try:
sed -i s/$find/$replace/g "$file"
Does not work (sed: 1: "Motorsport/Mücke": invalid command code M). Because of this error message I tried to escape my variables:
escaped_ find =$(printf '%s\n' "$find" | sed 's:[][\/.^$*]:\\&:g')
escaped_ replace =$(printf '%s\n' "$replace" | sed 's:[\/&]:\\&:g;$!s/$/\\/')
But usage of the replace variables does´t work ... I don´t know how to get this working :(
On the OSX version of sed, the -i option expects an extension argument so your command is actually parsed as the extension argument and the file path is interpreted as the command code.
Try adding the -e argument explicitly
sed -i'' -e 's/old/new/g' file
This question already has answers here:
How to use variables in a command in sed?
(4 answers)
Closed 6 years ago.
i would insert two variable in sed command:
sed -i '39,41 s/^#//' file
i would
sed -i '$LINE,$LINE_INCREMENTED s/^#//' file
but return this:
sed: -e expression #1, char 9: unknown command: `$'
Shell variables are not expanded when put inside single quotes, they are treated literally then.
Do:
sed -i "$LINE,$LINE_INCREMENTED"' s/^#//' file
Assuming the variables only contain digits.
As s/^#// part does not contain any shell expansion, putting double quotes over the full expression would do too, better readability:
sed -i "$LINE,$LINE_INCREMENTED s/^#//" file
drop the quotes for double quotes so environment variables are evaluated:
sed -i "$LINE,$LINE_INCREMENTED s/^#//" file
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