I'm trying to automate an install script for New Relic and in my bash file I have the following:
_APPNAME="Test Application"
_OLD=";newrelic.appname = \"PHP Application\""
_NEW="newrelic.appname = \"${_APPNAME}\""
sed -i 's/$_OLD/$_NEW/g' /etc/php.d/newrelic.ini
For some reason that sed command doesn't trigger at all, can anyone see anything wrong with this logic?
Note I have also tried ${_OLD} and ${_NEW} to no avail.
$_OLD and $_NEW are not expanded inside single quotes. '
Use double quotes " instead:
sed -i "s/$_OLD/$_NEW/g" /etc/php.d/newrelic.ini
Related
I am trying to write a shell script to find a replace this line of code on Ubuntu 14.04.
//Code before script runs
/*define('FS_METHOD','direct');*/
//Code after script runs
define('FS_METHOD','direct');
sudo sed -i "s#/*define('FS_METHOD','direct');*/#define('FS_METHOD,'direct');#g" /home/example/example.txt
After running this line of code I do not get any errors, but it does not remove the /* */ I believe this is because of the single quotes. I have tried escaping them with a backslash but it doesn't work either. Anyone have any idea how to get this to work?
No, the problem is that * is an RE metacharcter so you need to escape it to have it treated literally:
$ sed "s#/\*define('FS_METHOD','direct');\*/#define('FS_METHOD,'direct');#g" file
define('FS_METHOD,'direct');
or more concisely:
$ sed "s#/\*\(define('FS_METHOD','direct');\)\*/#\1#" file
define('FS_METHOD','direct');
I want to add some text on a line:
sudo sed -i '5imytext 16/16' /file
Now I've added mytext 16/16 on line 5 of the file, but I actually want to add the text 'mytext' 16/16 (mytext between single quotes).
I tried
sudo sed -i '5i'mytext' 16/16' /file
but it didn't work. Can someone help me?
The single quotes that you're trying to use in your insertion string are interfering with the ones around the sed command.
The simplest thing to do is to use different quotes around your sed command:
"5i'mytext' 16/16"
Normally it's best to use single quotes around a sed command but it would be more tricky in this case:
'5i'"'"'mytext'"'"' 16/16'
Basically, you need to put the single quotes inside double quotes somehow and in this case there's no reason not to double quote the whole command.
As suggested by 123 in the comments, an alternative would be to put your sed command into a script file:
5i'mytext' 16/16
Then use the -f switch to sed:
sed -f script
This avoids the need to use two kinds of quotes.
Use double quote in these cases. Because:
Single quote can't have single quote inside it. ('\'' won't work)
Double quote can have both single quote and double quote inside it. ("'\"" will work)
Example:
sudo sed -i "5i'mytext' 16/16" /file
You could use double quotes around your sed command, but that won't help you if you also need to insert double quotes. An alternative would be to use: \x27
Example: echo a|sed 's/a/\x27/' ➡ '
I'm implementing a template renderer in shell script. The template variables are represented in a template by #VAR_NAME# and their values are defined in a separate shell script.
Sample code:
# template variables values
CONTACT_EMAIL="myemail"
CONTACT_NAME="myname"
VARS="CONTACT_EMAIL CONTACT_NAME"
TEMPLATE_FILEPATH="mytemplate.txt"
# template renderer
set -x
SEDARGS=
for VAR in $VARS; do
SEDARGS+=" -e \"s,#$VAR#,${!VAR},\""
done
sed -r $SEDARGS $TEMPLATE_FILEPATH
sed command executed by shell and printed by it because of "set -x":
+ sed -r -e '"s,#CONTACT_EMAIL#,myemail,"' -e '"s,#CONTACT_NAME#,myname,"' mytemplate.txt
sed output:
sed: -e expression #1, char 1: unknown command: `"'
I know the single quotes around each sed expression are causing this non-intuitive error message, but I do not know why they are added.
What is wrong?
You have embedded quotes inside your SEDARGS variable. These are NOT removed when the command is executed. To remove them, you need to call the interpreter again, which you can do using eval. For example:
eval sed -r $SEDARGS $TEMPLATE_FILEPATH
You may need to play around that some more (adding quotes, etc.).
The single quotes aren't part of the actual arguments. They get added by your shell for the output caused by set -x only.
Why would the shell do that? So that you can use that output to re-run exactly what was executed during the script execution. As you correctly noticed, they are needed to protect the " that came from SEDARGS content (i.e., the inner ones, in your script escaped as \").
how can I use a variable as a pattern finder for sed? for example:
sed -i '/$pc/ s/off/on/' ~/Documents/Mantenimiento
I know there is a $ in between the '' but there gotta be a way! please help!
Use double quotes instead of single quotes, or close the quotes just before the variable and reopen them just after.
sed -i "/$pc/ s/off/on/"
sed -i '/'$pc'/ s/off/on/'
This will let bash perform the variable evaluation normally.
I believe this may be a simple question, but I've looked everywhere and tried some workarounds, but I still haven't solved the problem.
Problem description:
I have to replace a character inside a file and I can do it easily using the command line:
sed -e 's/pattern1/pattern2/g' full_path_to_file/file
But when I use the same line inside a bash script I can't seem to be able to replace it, and I don't get an error message, just the file contents without the substitution.
#!/bin/sh
VAR1="patter1"
VAR2="patter2"
VAR3="full_path_to_file"
sed -e 's/${VAR1}/${VAR2}/g' ${VAR3}
Any help would be appreciated.
Thank you very much for your time.
Try
sed -e "s/${VAR1}/${VAR2}/g" ${VAR3}
Bash reference says:
The characters ‘$’ and ‘`’ retain their special meaning within double quotes
Thus it will be able to resolve your variables
I use a script like yours... and mine works as well!
#!/bin/sh
var1='pattern1'
var2='pattern2'
sed -i "s&$var1&$var2&g" *.html
See that, mine use "-i"... and the seperator character "&" I use is different as yours.
The separator character "&" can be used any other character that DOES NOT HAVE AT PATTERN.
You can use:
sed -i "s#$var1#$var2#g" *.html
sed -i "s#$var1#$var2#g" *.html
...
If my pattern is: "test#email.com" of course you must use a seperator different like "#", "%"... ok?