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

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

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. ;)

How to use sed to replace text with a file path? [duplicate]

This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Closed 2 years ago.
I'm writing a bash script where I need to replace text in a file with a specific file path, but my understanding is that sed does not work with specific characters such as /. I'm wondering if there is some way around this?
Here is my script currently:
currentdir="$PWD"
filepathvar="${currentdir}/settings.ini"
sed -i -e "s/filepath/$filepathvar/g" aimparmstest
When I print out filepathvar everything is as I expect it to be, but it seems the fact that filepathvar contains special characters, it gives me the following error:
sed: -e expression #1, char 13: unknown option to `s'
Is there any way around this? Or perhaps another command I can use? I haven't had any success with changing around the parameters. Any help is greatly appreciated.
You can use any character as the separator (the first character). For example:
echo "a/b/c" | sed -e 's|/|_|g'
In your case:
sed -i -e "s|filepath|$filepathvar|g" aimparmstest

How do I escape double quotes in sed? [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 3 years ago.
Trying to figure out how to use a sed command to replace a value when the string i am searching for has double-quotes as part of the string.
I have a config file called ABC.conf that list out the release version like so; "ReleaseSequence": 1555
I want to use sed to change the release number to 2053
I have tried the following sed command;
sed -i 's:"ReleaseSequence":.*:"ReleaseSequence": 2053:' ABC.conf
I get the error message;
sed: -e expression #1, char 24: unknown option to `s'
I have tried to escape the doubel-quotes with [\"], '"""', and "" but sed doesn't like any of those.
The double quotes are not the problem here. You have used : as the separator although your data also contains literal colons. Use a different separator
sed -i 's#"ReleaseSequence":.*#"ReleaseSequence": 2053#' ABC.conf
or backslash-escape the literal colons.

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

Sed expression with escape sequence [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 6 years ago.
I need to replace the string '../lib' to '/usr/share/server/bootstrap/lib' in a file bootstrap.sh
I used the following sed expression
sed -i -e 's//././/lib///user//share//server//bootstrap//lib/g' bootstrap.sh
It fails with the log
sed: -e expression #1, char 6: unknown option to `s'
Unable to identity the mistake in the expression.Kindly help.
You need to escape every . and /
sed -i -e "s/\.\.\/lib/\/usr\/share\/server\/bootstrap\/lib/" bootstrap.sh
Alternative way to avoid two many backslashes using # as delimiter instead of /.
Thanks sp asic
sed -i "s#../libs#/usr/share/server/bootstrap/lib#" bootstrap.sh

Resources