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.
Related
How to use a variable instead of 3 in the command below?
sed -i '3s/$/ newvalue/' filename
I tried
var=1
sed -i '$vars/$/ newvalue/' filename
sed -i "$vars/$/ newvalue/" filename
First, you need to use double quotes to allow shell parameters/variables to expand. Then, you need to use braces to isolate the variable name if text that follows the variable could be interpreted as part of variable name (before${var}after). Finally, to use literal $ under double quotes, you should escape it a blackslash. All together:
var=3
sed -i "${var}s/\$/ newvalue/" filename
One alternative is to use alternating double and single quotes (under which no character is treated specially, including $ for parameter expansion):
sed -i "$var"'s/$/ newvalue/' filename
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 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
I have an variable
qsubFile="submitJob.sh"
echo $qsubFile returns submitJob.sh without the double quotes.
Now, I want to find the line containing the string qsubFile="someOtherFile.sh" and replace it to qsubFile="submitJob.sh" in the file "write.sh".
I tried using
sed -i '/qsubFile=/c\qsubFile="'"$qsubFile"'"' write.sh
qsubFile=""
I can't seem to get the proper syntax for this.
but it replaces it as
You just need single quotes for sed to do this, there is no problem with the double quotes inside the single quotes:
sed -i 's/qsubFile="someOtherFile.sh"/qsubFile="submitJob.sh"/g' write.sh
If "someOtherFile.sh" isn't a fixed string in write.sh than use the follow to replace them all:
$ sed -i 's/qsubFile="[^"]*"/qsubFile="submitJob.sh"/g' write.sh
Regex "[^"]*":
" # double quote
[^"]* # Anything not a double quote
" # double quote
Seems I misread the question the first time the correct quoting is to use the variable $qsubFile is, you missed the last /:
sed -i 's/qsubFile="[^"]*"/qsubFile="'"$qsubFile"'"/g' write.sh
You're somewhat on the right track if you need to use the shell variable though.
sed -i 's/qsubFile="someOtherFile.sh"/qsubFile="'"$qsubfile"'"/g' write.sh
or if you want to make sure you get the whole line
sed -i 's/^\(qsubFile=\).*$/\1"'"$qsubfile"'"/g' write.sh
This question already has answers here:
Environment variable substitution in sed
(12 answers)
sed substitution with Bash variables
(6 answers)
Closed 10 months ago.
I can't seem to use a variable in a sed command, for example:
sed "24s/.*/"$ct_tname"/" file1.sas > file2.sas
I want $ct_tname the variable, not literally $ct_tname, which is what I keep getting.
Anybody know how to get this to work?
The problem is actually more complex and I omitted some information.
ct_fname="%let outputfile="/user/ct_"$1".csv";"
Here, $1 is the argument passed in at the start of my bash script (sed is being run inside a bash script).
This doesn't run successfully, but it does run if I replace ct_fname with
ct_fname="%let table=ct_$1;"
Is there a way to get the first ct_fname to be passed successfully?
you need to use double quotes (") instead of single quotes (').
single quotes pass their content literally, without translating variables (expansion).
try
sed "24s/.*/\"$ct_tname\"/" file1.sas > file2.sas
btw, if you're going to be editing a file (that is if file2.sas is a temporary file), you should be using ed instead.
In my case, i just remplaced single quotes by the double ones:
for a in $(cat ext.cnf); do sed -n "/$a$/p" file1 >> file2; done
For now, it's working well...
The problem is that when $ct_fname is substituted, sed sees extra / separators, so
sed "24s/.*/"$ct_tname"/" file1.sas > file2.sas
becomes
sed "24s/.*/"%let outputfile=/user/ct_ARGUMENT1.csv;"/" file1.sas > file2.sas
and you'll get a sed error because there are 5 / instead of the expected 3.
Instead, change your sed separators to an unused character like | or :, and either single or double quotes will work just fine:
sed '24s|.*|'$ct_tname'|' file1.sas > file2.sas
sed "24s|.*|"$ct_tname"|" file1.sas > file2.sas
Shell variables are not expanded inside single quotes. Try this instead:
sed "24s/.*/\"$ct_tname\"/" file1.sas > file2.sas
You need to use double (") quotes, with single (') quotes the value of the variable doesn't get replaced. Since you have double quotes in your replacement text, you need to escape them:
sed "24s/.*/\"$ct_tname\"/" file1.sas > file2.sas
Other answers focus on the use of escaped double quotes in their examples. Note that this is not always what you want :
$ FOO="auie"; echo foo123bar|sed "s/123/\"$FOO\"/"
foo"auie"bar
$ FOO="auie"; echo foo123bar|sed "s/123/$FOO/"
fooauiebar
$ FOO="auie"; echo fooauiebar|sed "s/\"$FOO\"/123/"
fooauiebar
$ FOO="auie"; echo fooauiebar|sed "s/$FOO/123/"
foo123bar