Using double quotes and variables in sed command [duplicate] - bash

This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Closed 2 years ago.
I have two bash variables, one called GREP_ENTRY, which contains a long string:
GRUB_CMDLINE_LINUX="crashkernel=auto spectre_v2=retpoline rd.lvm.lv=d6c-2b-59-93-97-ea/lv_root rhgb quiet console=tty0
and the other is called NEW_ENTRY, which is the exact same as GREP_ENTRY but with fips=1 appended to the end:
GRUB_CMDLINE_LINUX="crashkernel=auto spectre_v2=retpoline rd.lvm.lv=d6c-2b-59-93-97-ea/lv_root rhgb quiet console=tty0 fips=1
I am trying to use sed to find GREP_ENTRY in /etc/default/grub and replace it with NEW_ENTRY. Currently, I am trying to use double quotes as follows:
sed -i "s/$GREP_ENTRY/$NEW_ENTRY/g" /etc/default/grub
But I get the error:
sed: -e expression #1, char 123: unknown option to `s'
I have tried using other combinations of double quotes but to no avail. Can someone explain how I can properly use sed in this situation?

Both variables contain /. That confuses sed's s command.
Switch from s/// to s|||.

Related

How to escape a character in a variable? [duplicate]

This question already has answers here:
Escape a string for a sed replace pattern
(17 answers)
Closed 1 year ago.
I have a script that passes a variable into a sed command like this:
sed "s-\t-&${SUBDIRECTORY}/-"
But if the variable contains - (dash), then the sed command throws an error.
So this script:
VARIABLE="test-variable"
sed "s-\t-&${VARIABLE}/-"
Results in this error:
sed: 1: "s-\t-&test-variable/-": bad flag in substitute command: 'v'
I have not been able to find any answers to this issue; it works fine without the -.
How can I fix this?
Use a shell parameter expansion that escapes each instance of -:
sed "s-\t-&${VARIABLE//-/\\-}/-"
In the Bash manual, under Shell Parameter Expansion:
${parameter/pattern/string}
The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. [...] If pattern begins with /, all matches of pattern are replaced with string. Normally only the first match is replaced. [...]
Proper escaping is a fairly difficult problem in the shell, but you could do something like:
$ variable="test-variable"
$ printf '\t\n' | v="$variable" perl -pe 's-\t-$ENV{v}-'
test-variable

Replace string containing slash using SED command [duplicate]

This question already has answers here:
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 3 years ago.
I am trying the below code to replace the string /IRM/I with E/IRM/I but am getting the file processed with no error and no transformation. I assume I'm using the cancel character incorrectly to allow the forward slash. Any help is much appreciated.
sed -i '/\/IRM\/IE\/IRM\/I/g'
A sed command needs to specify an operation (like s to replace), and that operation requires a sigil. You don't need to use a slash as that sigil.
printf '%s\n' 'This is a test: </IRM/I>' | \
sed -e 's#/IRM/I#E/IRM/I#g'
...correctly emits as output:
This is a test: <E/IRM/I>
Note that we added a s at the beginning of your sed expression, and followed it up with a # -- a sigil that isn't contained anywhere in the source or replacement strings, so you don't need to escape it as you would /.

Escaping parentheses in bash/sed [duplicate]

This question already has answers here:
Is it possible to escape regex metacharacters reliably with sed
(4 answers)
Closed 5 years ago.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
I've got some issues with escaping parentheses in a string, using bash and sed.
Here's what I'm doing:
#!/bin/bash
olddescription='(1 phrase/line)'
newdescription="\"$volledigenaam\""
(cd /home/hew/git/odoo/addons/$technischenaam ; sed -i s/$olddescription/$newdescription/g __openerp__.py)
I've read that if I use single quotes, I don't need to escape the parentheses.
I also tried escaping the parentheses with backslash but didn't work either.
This is the error:
sed: -e expression #1, char 4: unterminated `s' command
Any ideas on how to solve this?
Scape the slash in the value of olddescription:
olddescription='(1 phrase\/line)';
Then, you can try this:
sed -i "s/$olddescription/$newdescription/g" __openerp__.py

sed complains on numbers in bash script

I'm writing a script to replace dafault configuration values to user specified ones. But i'm getting unterminated 's' command errors from sed. After 2 Hours of googling i still haven't figured out whats causing this. The lines are:
CONFIG_GMETAD_COMPUTENODES="scccompute1 scccompute2"
CONFIG_GMETAD="/etc/gmetad.conf"
sed -i 's/localhost/'${CONFIG_GMETAD_COMPUTENODES}'/g' ${CONFIG_GMETAD}
this substitutes to
sed -i 's/localhost/scccompute1 scccompute2/g' /etc/gmetad.conf
error i get
sed: -e expression #1, char 23: unterminated `s' command
I don't see whats wrong in here, but haven't worked quite often with sed.
You need to quote your variables. Note how the sed command expands:
sed -i 's/localhost/'scccompute1 scccompute2'/g' /etc/gmetad.conf
This creates multiple options, not a single one with your sed script. Instead, try:
sed -i 's/localhost/'"${CONFIG_GMETAD_COMPUTENODES}"'/g' "${CONFIG_GMETAD}"
Containing the variable within double quotes causes the whole string to be treated as a single argument to sed, which is what you want. And of course, quotes around the filename because the filename might contain special characters. As a rule, always quote variables when you refer to them in bash.
Note that if the embedded variable contains slashes or some other special characters, it may break your script. So you might want to do some sanity checking before feeding it to sed:
CONFIG_GMETAD_COMPUTENODES="${CONFIG_GMETAD_COMPUTENODES//[^a-z0-9 ]/}"
This will strip all characters that are not alphanumeric or space, if bash is your shell.
The problem come from quoting in bash.
You should use
sed -i "s/localhost/${CONFIG_GMETAD_COMPUTENODES}/g"

How to evaluate variable within `sed` command? [duplicate]

This question already has answers here:
Environment variable substitution in sed
(12 answers)
SED not working [unterminated `s' command]
(3 answers)
Closed 7 years ago.
I have a variable called "num", that holds a number. I want to use it in "sed" like so:
sed '1,$(num)d' file.txt
also tried:
sed '1,($num)d' file.txt
...so that I can delete lines from 1 until the line num.
It gives me an error like this:
sed: -e expression #1, char 4: unknown command: `('
What is the correct way to do this? thank.
Your shell variable usage was incorrect. First, using the double quote ensures that the shell will expand the variable. Second, surrounding the variable in question (num) with the braces ensures that the variable will be seen by the shell as $num, instead of the subsequent d getting glommed on.
Here is how you should specify what you want to do:
sed "1,${num}d" file.txt
You can use single quotes but need to concatenate the script
num=42; seq 1 45 | sed '1,'$num'd'
will print
43
44
45
as expected.

Resources