How to add all special characters in variable for sed command - bash

I have a variable called varname which have characters such as /,. etc. Requirement is to use it with sed command. This is what I've done.
echo Hello admin, please add , after you enter the image name
read -p varname
sed -i "s/my-images=/&$varname/" /home/myconfig
echo Image $varname has been added to the configuration. Thanks!!
/home/myconfig has
id=1
max-mb=1000
my-images=customimage
And required output is
id=1
max-mb=1000
my-images=mynewtext/version1,customimage
I am getting error while running this code and the error is : sed: -e expression #1, char 28: unknown option to `s'
Any help would be appreciated.

You could escape all /'s in your variable with a backslash \ (a literal \ must be escaped as \\):
sed -i "s/my-images=/&${varname//\//\\/}/" /home/myconfig

if you have entered text with slash (as I can see you have mynewtext/version...), it will recognized as finish s command.
You can try to use another character as separator in sed, like:
sed -i "s#my-images=#&$varname#" /home/myconfig

Related

Bash: special characters in sed

I want to replace with sed in some bash script something like:
s:44:\"STRING\"
To:
s:NEWSTRING:\"NEWSTRING2\"
I tried many ways with escaping special characters, but I got always error
sed: -e expression #1, char 32: unterminateds' command`
or someting like that.
Can you please tell me the correct sed -i (sed -i "s/xxx/xxx/g" file) command for that?
You have to escape the backslashes properly:
sed 's/s:44:\\"STRING\\"/s:NEWSTRING:\\"NEWSTRING2\\"/'
Example:
$ echo 's:44:\"STRING\"' | sed 's/s:44:\\"STRING\\"/s:NEWSTRING:\\"NEWSTRING2\\"/g'
s:NEWSTRING:\"NEWSTRING2\"
You are missing the final delimiter. In your case it seems to be : therefore, you need to add a final : after your substitution content. It does not matter if you are using modification instruction like g

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"

Using variable with sed

First of all i apologise in case this has been answered before but i couldn't solve my problem.
I need to search a pattern and then replace it with a line of text comprising of both text and variable.Btw i am using bash..
say
$var = "stacko.ver/rulz=" **Note: $var contain double quotes & = & a dot and /**
i want to so the follow
1.Search for ;te.xt = Note: The value to be search contain ; & = and a dot
2.Replace it with
textnum=$var
Of course $var should be replaced with its actual value
My attempts
sed -i "s/;te.xt =/textnum=$var/" file
sed -i "s/;te.xt =/textnum="$var"/" file
sed -i "s/";te.xt ="/"textnum=$var"/" file
None of these actually worked , either sed giving me an error or the value of $var not shown in file
Thanks for the help
Regards
Quoting doesn't help since this is a sed issue, not a bash issue. Just pick a sed s-expression delimiter that doesn't appear in your text:
sed -i "s|;te.xt =|textnum=$var|" file
You can pick any delimiter for s that doesn't appear in your input. sed -e 'streetlight' is a perfectly valid sed command.
I can see the error:
$ var="stacko.ver/rulz="
$ data="foo ;te.xt = bar"
$ sed "s/;te.xt =/textnum=$var/" <<< "$data"
sed: -e expression #1, char 31: unknown option to `s'
The problem is that $var contains a slash, so sed's s/// command is breaking. You need to pick a character that does not appear in $var
$ sed "s#;te.xt =#textnum=$var#" <<< "$data"
foo textnum=stacko.ver/rulz= bar
This can be hard -- what if slash and hash are in $var? Using bash, you can use ANSI-C quoting to use a control character that is unlikely to appear in your data, e.g.
$ sed $'s\037;te.xt =\037textnum=$var\037' <<< "$data"
foo textnum=stacko.ver/rulz= bar

Resources