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

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

Related

Bash sed missing \ in variable [duplicate]

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

Using sed to replace a pattern matched with an exported variable [duplicate]

This question already has answers here:
Using variables in sed -f (where sed script is in a file rather than inline)
(5 answers)
Closed 3 years ago.
How can I use sed to replace the matched pattern with EXPORT variable? Or is there a way I can access shell script variables when invoking sed file?
shell_cript.sh
for i in ./data/*.sav;
do
read_input (){
read number_one number_two
read date1 inventory
read price
} < $i
read_input
export number_one
export i
export number_two
export inventory
export price
sed -f test.sed $2
done
test.sed
s/Filename/$i/g
s/1stplace/$number_one/g
s/2ndplace/$number_two/g
s/\$\$\$\$/$price/g
file.sav
Jonathan Lee
12/12/2019 2
1000
If you put all your sed commands in a sed script file, you will not be able to pass variables to it.
What you need to do is pass all your sed commands at the command line level and use double quotes " to have your bash/shell interpret the $ and do the proper variable substitutions. You do not need to export the variables as the variable substitution will be done by the shell before creating your sed subprocess.
sed 's/Filename/'"$i"'/g;s/1stplace/number_one/g;s/2ndplace/number_two/g;s/\$\$\$\$/price/g'

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.

Cannot redirect stdout to file specified by full path [duplicate]

This question already has answers here:
why sed replace + redirection deletes my file?
(3 answers)
Closed 7 years ago.
Let
CONFIG_FILE=${WWW_DIR}/${WORDPRESS_TEST_DIR}/wp-config.php
This doesn't work:
sed "${SED_ARG}" ${CONFIG_FILE} >| ${CONFIG_FILE}
(all I get is an empty file). It does not work either when I replace the variables with the actual path strings, nor when I escape the paths with ".
This does work:
sed "${SED_ARG}" ${CONFIG_FILE} >| wp-config.php
mv wp-config.php ${CONFIG_FILE}
so I would assume the sed call works just fine.
Strangely, this does work too:
echo TEST >| ${CONFIG_FILE}
so the sed part might be the problem after all. I am clueless. Any ideas?
The redirection opens the file for writing (not appending), which makes it a new empty file, before the sed command is executed.
You can use the -i flag of sed, to do an in place substitution:
sed -I "${SED_ARG}" "${CONFIG_FILE}"
I also put quotes around ${CONFIG_FILE} to avoid unexpected behavior if it contains any special characters.

Resources