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
Related
This question already has answers here:
How to use variables in a command in sed?
(4 answers)
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
I have this string in my file
$vnet_address
And i have in my bash run time this variable which i want to replace my string with:
$vnet_address=10.1.0.0/16
I get an error when i run this command:
sed -i "/\$vnet_address/ s//$vnet_address/"
And i believe it is because of the slash character '/' in my variable.
How to go about it?
You can use
sed -i "s,\\\$vnet_address,${vnet_address}," file
Note:
The regex delimiter character is replaced with ,
The $ is properly escaped with a literal \ character
See the online demo:
#!/bin/bash
s='$vnet_address'
vnet_address=10.1.0.0/16
sed "s,\\\$vnet_address,${vnet_address}," <<< "$s"
# => 10.1.0.0/16
This question already has answers here:
Linux sed command - using variable with backslash
(2 answers)
Closed 1 year ago.
I'm trying to replace all \ with \\ in bash, I'm doing it like this but bash gets stuck in a never-ending loop. Where am I going wrong?
myVar="${myVar//\//\\\\}"
You can use sed for that:
echo "hello\world\hello\world" | sed 's/\\/\\\\/g'
Outputs:
hello\\world\\hello\\world
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|||.
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 /.
This question already has answers here:
Replacing some characters in a string with another character
(6 answers)
Closed 3 years ago.
I need to replace the special character which is not alphanumeric with a backslash in a string.
How do i do it in Bash? My version is 4.1
I can capture the special character the plus symbol using the following regex
([^[:alnum:]])
For example, applied to the string
Alan5+6imson
I can do
$ echo $orig_str |sed 's/([^[:alnum:]])/\\1/g'
Alan5+6imson
I need the output as
Alan5\+6imson
How can I replace it in Bash?
I tried the above regex but not sure how to perform a replacement.
Do i need to use some other tool or something like sed?
Would you please try:
echo "$orig_str" | sed 's/\([^[:alnum:]]\)/\\\1/g'
or:
echo "$orig_str" | sed 's/[^[:alnum:]]/\\&/g'