Home > Enterprise >  gsed replace by a variable $i with single quote
gsed replace by a variable $i with single quote

Time:06-03

I have into a text file the following line :

\[Omega]BD=100;

I would like to replace with gsed the value 100 by a shell variable (zsh shell), here 600 :

I tried :

$ i=600

$ gsed  's/\[Omega]BD=.*/\[Omega]BD=\'\\"$i"\\';/' text_to_modify.txt | grep 600

but it returns me :

\[Omega]BD=\600; and not \[Omega]BD=600;

The is an additional backslash that I don't want, I wonder how could I remove this backslash. I would like to keep the 2 single quotes of gsed 's/.../.../'

Any idea ?

CodePudding user response:

You may use this sed command:

i=600
sed -E "s/(\\\\\[Omega]BD=).*/\1$i/" file

\[Omega]BD=600

We require additional escaping i.e. \\\\ to match a single \ because we are using double quotes around full sed command.

Or we can avoid you can use this combination of single and double quotes to avoid extra escaping:

sed -E 's/(\\\[Omega]BD=).*/\1'"$i/" file

CodePudding user response:

Using sed;

i=600
$ sed "/\[Omega]/s/[[:digit:]]\ /$i/" input_file
\[Omega]BD=600;
  • Related