Bash sed missing \ in variable [duplicate] - linux

This question already has answers here:
Escape a string for a sed replace pattern
(17 answers)
Closed 3 years ago.
I have some code like
#!/bin/bash
DIR='/project/stnb'
HOST_CONFIG="${DIR}/config/host.$(hostname -s).php"
CT_NUMBER="$(grep -Pow --max-count=1 ct'[0-9]*' ${HOST_CONFIG})" #number of container
BASH_PROFILE_PS_STING="PS1='\[\e[0;32m\]\u\[\e[0;32m\]#\[\e[0;36m\]${CT_NUMBER}\[\e[0;33m\]\w\[\e[0;36m\]\$(__git_ps1 \"(%s)\") \[\e[0m\]\$ '"
so I want to rewrite 1st row at "bash_profile", and try "sed"
sed -i "1с${BASH_PROFILE_PS_STING}" ~/.bash_profile
but after replacing I see only
PS1='[e[0;32m][e[0;32m]#[e[0;36m]ct88[e[0;33m]w[e[0;36m]$(__git_ps1 "(%s)") [e[0m]$ '
After googling I was try following options:
1) Use an alternate regex delimiter
sed -i "1s!.*!${BASH_PROFILE_PS_STING}!" ~/.bash_profile
2) or
sed -i "1c~${BASH_PROFILE_PS_STING}" ~/.bash_profile
what's wrong?
Is it really necessary to preliminary replace \ in variable before
and replace change after operation back?

You need to specify the replacement string, like:
sed -i 's!OLD_STRING!NEW_STRING!' ~/.bash_profile

Related

How to replace path stored in variable using sed [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 12 months ago.
I've a script
#!/bin/bash
mv /home/nnice/Downloads/Images/* /run/media/nnice/New\ Volume/FILES/Softwares/HTTP/Downloads/Images
mv /home/nnice/Downloads/Video/* /run/media/nnice/New\ Volume/FILES/Softwares/HTTP/Downloads/Videos
I need to replace path using sed command. I've stored paths in variables
replace="/run/media/nnice/New\ Volume/FILES/Softwares/HTTP/Downloads/"
replacewith="/home/nnice/Windows/D/FILES/Softwares/HTTP/Downloads/"
I am trying following command but it doesn't work
sed -i "s/$replace/$replacewith/g" script.sh
I've also used different separators instead of / but script remains unchanged.
[nnice#myhost scripts]$ sed "s|$replace|$replacewith|g" script.sh
#!/bin/bash
mv /home/nnice/Downloads/Images/* /run/media/nnice/New\ Volume/FILES/Softwares/HTTP/Downloads/Images
mv /home/nnice/Downloads/Video/* /run/media/nnice/New\ Volume/FILES/Softwares/HTTP/Downloads/Videos
can you please help me with that to replace them using sed command?
Thank you
Your command fails because you're using the same separator for the sed command and your file paths. File paths need to use / but sed separators can be anything, so try this:
sed -i "s#$replace#$replacewith#g" script.sh

SED replace a word inside a bracket [duplicate]

This question already has answers here:
What is the difference between `sed -i -e` and `sed -ie`?
(2 answers)
Closed 2 years ago.
I need to change some words in bulk, but because of the brackets inside it, I think I do something wrong.
Line that needs to be changed
echo "CMD_PLUGINS_ADMIN/admin/index.html";
I need to change it to this:
echo "CMD_PLUGINS_ADMIN/reseller/index.html";
I tried it with: sed -ie 's/admin/reseller/' *
But does not change anything, I hope someone knows the right command for it.
$ echo '"CMD_PLUGINS_ADMIN/admin/index.html";' | sed 's/\/admin\//\/reseller\//g'
"CMD_PLUGINS_ADMIN/reseller/index.html";
your input has slash and you are using slash as sed seperator
Either escape the slashes in input by preceeding them with backslash:
echo '"CMD_PLUGINS_ADMIN/admin/index.html";' | sed 's/\/admin\//\/reseller\//g'
"CMD_PLUGINS_ADMIN/reseller/index.html";
OR change seperator to any other like pipe:
echo '"CMD_PLUGINS_ADMIN/admin/index.html";' | sed 's|admin|reseller|g'
"CMD_PLUGINS_ADMIN/reseller/index.html";

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

how to use sed command properly to replace values containing / delimiter [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 5 years ago.
File: abc.properties
tomcat.home=/opt/tomcat
Set to /usr/local/tomcat. Following cmd is working.
sed -i "/tomcat.home=/ s/=.*/="usr\\/local\\/tomcat"/" abc.properties
Set to $WORKSPACE/tomcat. Following cmd is NOT working since value of the $WORKSPACE is having / delimeters.
sed -i "/tomcat.home=/ s/=.*/="$WORKSPACE\\/tomcat"/" abc.properties
Anyone has an idea how to success above cmd.
Thank you and appreciate your support...
Sed lets you use any character you want as the delimiter. Whatever follows the s is used as the separator:
sed -Ee 's/foo/bar/'
sed -Ee 's|foo|bar|'
sed -Ee 's#foo#bar#'
^- All of those are equivalent.
The other option is to escape all your / as \/, but that gets nightmarish fast. Prefer to just pick a separator character that doesn't collide with characters you're trying to use for something else.

Replace a text with a variable [duplicate]

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.

Resources