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.
Related
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"]
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:
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
This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 6 years ago.
I'm trying to replace text in a file with the output of another command. Unfortunately, the outputted text contains characters bash expands. For example, I'm running the following script to change the file (somestring references output that would break the sed command):
#!/bin/bash
somestring='$6$sPnfj/lnXwZVrec7$fCnL9uy1oWIMZduInKTHBAxhsQxGCsBpm2XfVFFqDPHKidrd93yfjbYvKgYexXHVcvkKdu9lbfy16Ek5GvKy/1'
sed '0,/^title/s/^title*/'"$somestring"'\n&/' $HOME/example.txt
sed fails with this error:
sed: -e expression #1, char 30: unknown option to `s'
I think bash is substuting the contents of $somestring when building the sed command, but is then trying to expand the resulting text. I can't put the entire sed script in single quotes, I need bash to expand it the first time, just not the second. Any suggestions? Thanks
here the forward slash / is the problem. If it's the only issue you can set sed to use a different delimiter.
for example
$ somestring="abc/def"; echo xxx | sed 's/xxx/'"$somestring"'/'
sed: -e expression #1, char 11: unknown option to `s'
$ somestring="abc/def"; echo xxx | sed 's_xxx_'"$somestring"'_'
abc/def
you also need to worry about & and \ chars and escape them if can appear in the replacement text.
If you can't control the the replacement string, either you have to sanitize with another sed script or, alternatively use r command to read it from a file. For example,
$ seq 5 | sed -e '/3/{r replace' -e 'd}'
1
2
3slashes///1ampersand&and2backslashes\\end
4
5
where
$ cat replace
3slashes///1ampersand&and2backslashes\\end
You have several errors here:
the string somestring has characters that are significative for sed command (the most important being '/' that you are using as a delimiter) You can escape it, by substituting it with a previous
somestring=$(echo "$somestring" | sed -e 's/\//\\\//g')
that will convert your / chars to \/ sequences.
you are using sed '0,/^title/s/^title*/'"$somestring"'\n&/' $HOME/example.txt which is looking to substitute the string titl followed by any number of e characters by that $somestring value, followed by a new line and the original one. Unfortunately, sed(1) doesn't allow you to use newline characters in the pattern substitution side of the s command, but you can afford the result by using the i command with a text consisting of you pattern (preceding any new line by a \ to interpret it as literal):
Finally the script leads to:
#!/bin/bash
somestring='$6$sPnfj/lnXwZVrec7$fCnL9uy1oWIMZduInKTHBAxhsQxGCsBpm2XfVFFqDPHKidrd93yfjbYvKgYexXHVcvkKdu9lbfy16Ek5GvKy/1'
somestring=$(echo "$somestring" | sed -e 's/\//\\\//g')
sed '/^title/i\
'"$somestring\\
" $HOME/example.txt
If your shell is Bash, you can use parameter substitution to replace the problematic /:
somestring="{somestring//\//\\/}"
That looks scary, but is easier to understand if you look at the version that replaces x with __:
somestring="${somestring//x/__}"
It might be easier to use (say) underscore as the delimiter for your sed s command, and then the substitution above would be
somestring="${somestring//_/\\_}"
If you already have backslashes, you'll need to first replace those:
somestring="${somestring//\\/\\\\}"
somestring="{somestring//\//\\/}"
If there were other characters that needed escaping (e.g. on the search side of s///), then you could extend the above appropriately.
This URL provides the cleanest answer:
Command to escape a string in bash
printf "%q" "$someVariable"
will escape any characters you need escaped for you.