Replace a text with a variable [duplicate] - linux

This question already has answers here:
sed substitution with Bash variables
(6 answers)
Closed 7 years ago.
How can I do this?
sed -i 's/wiki_host/$host_name/g' /root/bin/sync
It will replace wiki_host with the text $host_name.
But I want to replace it with the content of the variable..
I tried it with
sed -i 's/wiki_host/${host_name}/g' /root/bin/sync
It doesn't work either.

You need to use double quotes:
$ sed -i "s/wiki_host/${host_name}/g" /root/bin/sync
Your single quotes prevent the shell variable from being replaced with its contents.

Related

Replacing string in a file by bash variable [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 6 months ago.
How to replace a string in a file by bash variable, please
I tried
i="1"
sed -i 's/%q/0$i/g' file
sed -i 's/%q/0"$i"/g' file
There is 0$i or 0"$i" in the file instead of 01. Thank you
Change your script as follow:
i="1"
sed -i "s/%q/0$i/g" file
sed -i "s/%q/0\"$i\"/g" file
and remember: the shell variables substitution does not happen inside single quotes. ;)

Linux sed command is not working when trying to substitute environment variable [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 4 years ago.
I have an environment variable LIB_HOME=/etc/temp
In one of the config files, i'm trying to replace entire line using sed command as follows
sed -i '/lib.home=/c\lib.home=$LIB_HOME' myconfig.properties
Actual Output : lib.home=blablabla ===> lib.home=$LIB_HOME
Expected Output : lib.home=blablabla ===> lib.home=/etc/temp
Please help
Have you tried using double quotes?
sed -i '/lib.home=/c\lib.home=‘“$LIB_HOME”' myconfig.properties

How to define $i in linux bash shell? [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 4 years ago.
I am having a problem while using the bash shell. Here is my linux command code:
for i in `cat linshi`;do sed -i '/$i/d' a.txt;done
The content of linshi is:
aa
bb
The content of a.txt is:
aa:wwersdf12314231234
bb:weorpius2345234523
cc:ertoiu230498234098
dd:234092834asdfkdfkg
I want to delete the first and the second row of a.txt.
But unlucky, I found '/$i/d' is not correct. And I have tried '/\$i/d' and '/"\"$id/', but they are fail again. Who can help me?
Variables aren't expanded inside single quotes, only double quotes.
for i in `cat linshi`; do sed -i "/$i/d" a.txt; done
That said, you could do the same thing with:
grep -vf linshi a.txt
Instead of using single quotes use double quotes. '' doesn't undergo any variable expansion however double quotes do.
This will work:
for i in $(cat linshi);do sed -i "/$i/d" a.txt;done

Replacing text with sed containing whitespace in variable [duplicate]

This question already has answers here:
sed substitution with Bash variables
(6 answers)
how to find replace value with whitespace using sed in a bash script
(2 answers)
Closed 5 years ago.
I try to replace some text within a text file. In my script I have store the text to find and to replace within two variables:
$currentProductName='hello'
$configProductName='byebye'
The replacing is done with this line:
sed -i'.bak' 's/$currentProductName/$configProductName/g' "$projectFile"
Everything works fine until any of my variables are containing whitespaces. If the $configProductName is set to hello world the sedcommand does not work as expected.
I´ve already tried this but it doesn't work, too:
sed -i'.bak' 's/"$currentProductName"/"$configProductName"/g' "$projectFile"
sed -i'.bak' 's/\$currentProductName/\$configProductName/g' "$projectFile"
How do I must change the line to work as expected?
To preserve the spaces, you need to double-quote the variable and wrap it over once again with single quotes. This way you have control over which variables prefixed with $ needs to be expanded.
configProductName='hello world'
Use the sed operation just as below and add the -i flag once you find it working as expected.
sed 's/$currentProductName/'"$configProductName"'/g' file
hello world='hello'
$configProductName='byebye'

Insert variable in specific place using sed -i [duplicate]

This question already has answers here:
How to use variables in a command in sed?
(4 answers)
Closed 6 years ago.
i would insert two variable in sed command:
sed -i '39,41 s/^#//' file
i would
sed -i '$LINE,$LINE_INCREMENTED s/^#//' file
but return this:
sed: -e expression #1, char 9: unknown command: `$'
Shell variables are not expanded when put inside single quotes, they are treated literally then.
Do:
sed -i "$LINE,$LINE_INCREMENTED"' s/^#//' file
Assuming the variables only contain digits.
As s/^#// part does not contain any shell expansion, putting double quotes over the full expression would do too, better readability:
sed -i "$LINE,$LINE_INCREMENTED s/^#//" file
drop the quotes for double quotes so environment variables are evaluated:
sed -i "$LINE,$LINE_INCREMENTED s/^#//" file

Resources