using sed with local variables that have / - bash

I'm trying to make the sed command replace these:
CURRENT_TOMCAT=/opt/tomcat/lib
NEW_TOMCAT=/usr/tomcat/lib
sed -i "s/$CURRENT_TOMCAT/$NEW_TOMCAT/" tomcat.env
I've tried a bunch of combinations with no success, sometimes getting the unknown option to `s' error, sometimes getting no errors, but no changes are made
The line I that needs to be change looks like this:
TOMCAT_LIB=/opt/tomcat/lib; export TOMCAT_LIB

Try changing the delimiter to another one not contained in your strings. For example, #:
CURRENT_TOMCAT=/opt/tomcat/lib
NEW_TOMCAT=/usr/tomcat/lib
sed -i "s#$CURRENT_TOMCAT#$NEW_TOMCAT#" tomcat.env
^ ^ ^
Test
$ cat a
hello/how are you?
$ old="hello/how"
$ new="bye/how"
$ sed "s/$old/$new/" a
sed: -e expression #1, char 13: unknown option to `s' <---- meeec
$ sed "s#$old#$new#" a
bye/how are you? <---- successful!

Related

sed: Bad flag in substitute command '('

I'm trying to replace a placeholder ${SNIPPET} with the content of a js file. I struggle to understand the error I receive though.
sed -e "s/\${SNIPPET}/$(cat snippet.js)/" ../../handlebars/templates/bootstrap-template.hbs
Error: bad flag in substitute command: '('
Looking for a solution that could work cross-platform (OSX/Linux)
Using these test files
$ cat snippet.js
hello/(world)
$ cat template.hbs
foo
${SNIPPET}
bar
I can (kind of) replicate your error (I have GNU sed 4.2.2):
$ sed "s/\${SNIPPET}/$(cat snippet.js)/" template.hbs
sed: -e expression #1, char 20: unknown option to `s'
You can do this, which escapes slashes (which are the delimiter for the s/// command)
sed "s/\${SNIPPET}/$(sed 's,/,\\/,g' snippet.js)/" template.hbs
foo
hello/(world)
bar
Or, if the SNIPPET placeholder is on its own line like I have it, you can use other sed commands:
sed '/\${SNIPPET}/{
# read the file into the stream
r snippet.js
# delete SNIPPET line
d
}' template.hbs
foo
hello/(world)
bar
Yet another approach
j=$(<snippet.js) # read the file: `$(<...)` is a bash builtin for `$(cat ...)`
sed "s/\${SNIPPET}/${j//\//\\\/}/" template.hbs

sed command containing variable not working

I can't get the syntax for this command right... I need to change a variable in a file where the variable contains a path.
sessionFolderName=/session_`date '+%y%m%d'`_`date '+%H%M'`
sed "s/sessionFolder=.*/sessionFolder=/"$sessionFolder /home/pi/scripts/settings/settings.sh > tmp
mv tmp /home/pi/scripts/settings/settings.sh
However the result is:
sed: -e expression #1, char 35: unknown option to `s'
# You don't need to (and in your case, should not) invoke date command twice.
# Try running following in bash to see the problem.
# $ echo $(date '+%s')_$(sleep 1)_$(date '+%s')
# On a different note, it's better to use $(...) instead of backticks.
sessionFolderName="/session_$(date '+%y%m%d_%H%M')"
# You can use several other separators in sed.
# e.g. :, ;, #, #, _ and even a space
sed "s:sessionFolder=.*:sessionFolder=${sessionFolder}:" /home/pi/scripts/settings/settings.sh > tmp
mv tmp /home/pi/scripts/settings/settings.sh
Refer to this regarding using $() instead of backticks
The problem is with the / char in the start of your variable. This interrupts sed syntax:
[root# ~]# sed "s/sessionFolder=.*/${sessionFolderName}/" text
sed: -e expression #1, char 21: unknown option to `s'
[root# ~]# echo $sessionFolderName
/session_170824_0942
If you escape it using double backslash - \\, it works:
[root# ~]# sed "s/sessionFolder=.*/sessionFolder=\\${sessionFolderName}/" text
sessionFolder=/session_170824_0942
1) You need to escape the / in your input
2) modify the format of the sed command as shown below
sessionFolderName=\\/session_`date '+%y%m%d'`_`date '+%H%M'`
sed "s/sessionFolder=.*/sessionFolder="$sessionFolderName"/" /home/pi/scripts/settings/settings.sh > tmp

Replace a variable with text (sed)

I have to find a specific text in the file and then replace that text with some new text.
The contents of the file are:
host=100
servers=4
clients=70
I have tried this:
var=$(grep "servers=" /path/to/file)
sed -i "s/${var}/servers=5/g" /path/to/file
But it gives me the error:
sed: -e expression #1, char 2: unterminated `s' command
Note: All I want is to update the value of each of the variable i.e. servers=4 should be replaced by servers=5.
Please help me figure out the solution.
Thanks.
The output of grep ends with a newline character. sed expects the whole command on one line or escaping line breaks.
However, you can easily achieve the complete task with sed only:
sed -i 's/^servers=[0-9]*$/servers=5/' /path/to/file
sed -i.bak "s/servers=[0-9]*/servers=5/" /path/to/file

Find and Replace string using sed gives error

I am using shell script. My requirement is to find and replace the string. The string contains "/" char as well. I am getting error sed: -e expression #1, char 18: unterminated `s' command. Can someone tell how should i replace the string which has "/"?
#!/bin/bash
...
search_string="../conf/TestSystem/Inst1.xml"
rep="Inst1/Instrument.xml"
sed -i 's|${line}|${rep}/g' MasterConfiguration.xml
I tried using another sed command but that one also gave error sed: -e expression #1, char 13: unknown option to `s'
sed -e "s/${line}/${rep}/g" MasterConfiguration.xml > tempfile
Whenever you deal with shell-variables you have to get them out of the "sed-string":
For example:
sed -e "s/"${line}"/"${rep}"/g" MasterConfiguration.xml > tempfile
Otherwise sed will treat the chars as-is and search for ${line} literally:
As you see, nothing happens here.
Furthermore, if your variables contain / you need to use another delimiter for sed. I tend to use ~ in such a case, but you're free to use other chars - just be consequent and don't mix them like in your first example-sed-command:
sed 's~'${line}'~'${rep}'/g' //WRONG
sed 's~'${line}'~'${rep}'~g' //RIGHT
Combine both and it will work:
You can try this sed,
sed -i "s#${line}#${rep}#g" MasterConfiguration.xml
Problem:
Instead you have,
sed -i "s|${line}|${rep}/g" MasterConfiguration.xml
It should be,
sed -i "s|${line}|${rep}|g" MasterConfiguration.xml
Syntax:
sed "s|pattern|replacement|g"

Single quoted string for sed in Makefile

I am calling sed from my Makefile, and this line doesn't work:
sed -i -r 's|^(3)$|5|' file;
It's perfectly fine if I call from the terminal, but GNU make reports the following error:
sed: -e expression #1, char 8:
unterminated `s' command
Any ideas how I can fix this, and could you provide any manual on how Makefile deals with strings? Thanks.
$ has a special meaning in makefiles. Use $$ cancel it:
sed -i -r 's|^(3)$$|5|' file;

Resources