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

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'

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

How to store part of the file name into a variable using shell script? [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
How do I remove the file suffix and path portion from a path string in Bash?
(15 answers)
Closed 1 year ago.
I have a file 20210823_Club_Member_Name_by_ID.txt. I want to get only the first element of the file name which is 20210823 and store it into a variable using shell script.
Currently, my code can print out the first element in the terminal but I also want to store it into a variable for further usage.
file='20210823_Club_Member_Name_by_ID.txt'
echo "$file" | awk -F'[_.]' '{print $1}'
// I try to store it like below, but it does not work
fileDate= echo "$file" | awk -F'[_.]' '{print $1}'
echo $fileDate
As Jetchisel commented, you can use shell parameter expansion to safely extract the value. The %% operator removes as much of the matching text as possible, starting from the end of the string; if we use _* then this will essentially remove everything after and including the first underscore.
file='20210823_Club_Member_Name_by_ID.txt'
fileDate="${file%%_*}"
The fileDate variable will now contain 20210823.

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'

how to use variables in SED [duplicate]

This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
Closed 8 years ago.
I have an xml file with line below
<config-root>/temp</config-root>
Using 'sed' is bash shell script I want to replace the line,
the 'sed' script is below
sed -i 's/<config-root>\(.*\)<\/config-root>/<config-root>\"${dirPath}"<\/config-root>/' Plan.xml
The 'sed' is resulting in
<config-root>"${dirPath}"</config-root>
I am expecting the line to be replaced as /opt/shared
Can anyone let me know what is wrong in my script? Basically I want to use variable in ‘sed’
Thanks in advance,
Babu
You can use bash to place the variable in the sed script: End the sed script using the single quote ', place the variable in double quotes " and continue the sed program with another single quote ':
sed 's~<config-root>[^<]*</config-root>~<config-root>'"$variable"'</config-root>~' Plan.xml
I would encourage you to use delimiter different from / because the / is part of the pattern (and of the variable) and would need to get escaped otherwise. I used ~ as the delimiter.

Resources