sed command not working correctly and gives the error unknown command - bash

I am trying to replace a string in a file using the SED command.
Here the command is \
sed -i 's/\\F\\//g' $filepath
this works completely fine and when I do the same thing in the below way
param=s/\\F\\//g
sed -i "'$param'" $filepath
it give this error:
sed: -e expression #1, char 1: unknown command: `''
and some times other error saying path not found if I try to play with the syntax.
Thanks

Put the param as-is in a variable. Then use the variable, as a rule of thumb, always inside ".
param='s/\\F\\//g'
sed -i "$param" "$filepath"

Related

How to add all special characters in variable for sed command

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

Bash sed delete line from file not working

I am trying to delete a line from a text file that has a matching ID number.
Student id variable: $sid, for example 12345678;
$FILE = student_record
I first tried:
sed -i '/$sid/d' student_record.txt
Which gave me file not found. Next:
sed -i '/$sid/d' $FILE
And I get: sed: 1: "student_record": unterminated substitute in regular expression
sed -i '/12345678/d' $FILE
Same error as above
sed -i '/$sid/ d' student_record.txt
yields:
sed 1: "student_record.txt": bad flag in substitute command: 'x'
If I try without -i,
sed '/$sid/ d' $FILE
It just prints the whole file and doesn't delete any lines.
Advice would be great.
If the file is called student_record as you say for $FILE, you may be making a mistake using student_record.txt which would explain while you get file not found.
For many of the others, if you use single quotes it will not expand variables, so you'll literally be looking for the string "$sid". If you use double quotes it will expand, so try
sed -i "/$sid/d" "$FILE"
assuming you have GNU sed. If you're on something that does not have GNU, you may not have -i or it may require an argument.

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 sed with local variables that have /

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!

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