I have a problem with my following script (this is the relevant part of it):
#!/bin/bash OLD=( "_MAIN1_" "_MAIN2_" ) NEW=( "#111" "#222" ) length=${#OLD[*]} i=0 while (( i < length )) do sed -e "s/${OLD[$i]}/${NEW[$i]}/g" oldfile.txt > newfile.txt #sed -e 's/_MAIN1_/#111/g' oldfile.txt > newfile.txt # this works # Another way that does not work #sed -e 's/'"${OLD[$i]}"'/'"${NEW[$i]}"'/g' oldfile.txt > newfile.txt ((i++)) done exit 0
My goal is to replace strings in a file and save it into a new one. The "old" and "new" strings are stored in an array.
I tried a lot of things and played around with single and double quotes - but nothing worked. When I echo
the variables I get the correct strings inside the loop. If explicit two strings are set in sed
command it works fine for this.
The string patterns follow those in my example arrays ('new' contains the underscore "_" and 'old' contains the hashtag "#").
I'm running bash on a Ubuntu 16.04 box.
Thank you very much!
-i
option in sed ormv
newfile.txt -> oldfile.txt after every sed operation.