Reputation: 159
How to I can use, shell/bash script's 'sed', to replace string with content of variable and surround it by acute/backslash.
For example:
file (file.txt) containing line
FILE_PATH="this/is/the/file/path"
to be replaced like
FILE_PATH=`get/file/path/get.sh FILE_NAM`
I tried the following sed, but did not work for me
SCRIPT_PATH="get/file/path/get.sh FILE_NAM"
sed -i "s,\"this/is/the/file/path\",`$SCRIPT_PATH`,g" $file
above resulted
FILE_PATH=get/file/path/
but expected is
FILE_PATH=`get/file/path/get.sh FILE_NAM`
Can someone please help me to get replace string with content of a variable with acure around value of variable.
Upvotes: 0
Views: 1260
Reputation: 3229
If the issue is purely with sed
, your command isn't formatted quite correctly. It looks like the overall issue is you aren't printing the escape sequence for the ` character, which is a special character in bash for executing shell commands within another command.
Based on your desired results and sample code, the following works for me in bash.
SCRIPT_PATH="this/is/the/file/path"
sed -i "s|\"this/is/the/file/path\"|\`$SCRIPT_PATH\`|g" file.txt
Upvotes: 3
Reputation: 159
I found the answer as below:
SCRIPT_PATH="get/file/path/get.sh FILE_NAM"
sed -i "s,\"this/is/the/file/path\","\`"$SCRIPT_PATH"\`",g" $file
Upvotes: 0