Would it be even possible to do it without some sort of script just in one-line command in bash?
Your question somehow triggered a burning ambition in me to do this...!
varfile=SOURCEFILE && varsubstfile=RESULTFILE && IFS=' ' read -a repl <<< $(sed -r 's/(.*)(ref="#.*?")( .*)/\2/;tx;d;:x' $varfile | sed -e 's/\ /\-/g' | sed ':a;N;$!ba;s/\s/ /g') && for i in "${!repl[@]}"; do needle["$i"]=$(sed 's/\-/\ /g' <<< "${repl["$i"]}"); done && cp $varfile $varsubstfile && for i in "${!needle[@]}"; do sed -ir "s/${needle[i]}/${repl[i]}/g" $varsubstfile; done && unset needle && unset repl && less $varsubstfile && unset varfile && unset varsubstfile
SOURCEFILE
is your sourcefile, RESULTFILE
is the name of a file where the output gets written to, so change both of them according to your needs.
Well... it is kind of a script, but it's a (damn huge) one-liner :)
I supposed that there are more occurences of ref="#.*"
in the whole file, otherwise it would have been much shorter (although I don't remember the shorter version anymore).
... and I really hope this works on your *nix-system :D
Just in case you want to know what this thing does, here's an explanation:
varfile=SOURCEFILE && #set variable for the sourcefile
varsubstfile=RESULTFILE && #set variable for the resultfile
IFS=' ' read -a repl <<< #we're going to read multiple values into an array "repl"
#delimited by a space
$(
#grab only the second capture group (ref="#.*?")
sed -r 's/(.*)(ref="#.*?")( .*)/\2/;tx;d;:x' $varfile |
sed -e 's/\ /\-/g' | #replace every space in (ref="#.*?") with a dash
sed ':a;N;$!ba;s/\s/ /g' #replace newlines with a space
#when there is more than one occurence sed will delimit them with a newline
#but i set a space as the delimiter for the read operation,
#thus the last replacement
) &&
#we now have every needed replacement-string in an array called "repl"
for i in "${!repl[@]}"; do #iterate over every value in the array we just read
needle["$i"]=$(sed 's/\-/\ /g' <<< "${repl["$i"]}"); #replace dashes with spaces and store in a new variable
done &&
#and now every original string, the needle we are going to search for
#is stored in another array
cp $varfile $varsubstfile && #copy sourcefile to resultfile
for i in "${!needle[@]}"; do #for every string we are going to replace
sed -ir "s/${needle[i]}/${repl[i]}/g" $varsubstfile; #... we replace it!
done
#technically we're done here
#but i like to clean up afterwards and show the result with less
unset repl && less $varsubstfile && unset varfile && unset varsubstfile