I'm trying to use sed to replace template strings in files of the form %XXX% with the value of a variable called XXX in my shell script.
e.g. The following works perfectly
sed "s/%user_home%/$user_home/gi"
So if user_home=fred the following,
NameVirtualHost *:80
<VirtualHost *:80>
ServerName %server_name%
ErrorLog /var/log/apache2/%user_home%_webapp_error.log
CustomLog /var/log/apache2/%user_home%_webapp.log common
DocumentRoot /home/%user_home%/web_app/public
</VirtualHost>
becomes,
NameVirtualHost *:80
<VirtualHost *:80>
ServerName %server_name%
ErrorLog /var/log/apache2/fred_webapp_error.log
CustomLog /var/log/apache2/fred_webapp.log common
DocumentRoot /home/fred/web_app/public
</VirtualHost>
The problem is that I want to run the sed command without explicitly knowing the template strings and their variables up front. That is, it looks for %XXX% and then replaces that with the contents of $XXX without caring what the actual name of the variable is.
I know its got something to do with back-references but I can't figure out how to use the content of a back-reference as the variable name.
I tried,
sed "s/%\([a-z_]\)%/$(\1)/gi"
but this failed to work because it seems to be a looking for a variable called $\1.
sed.$ENV{$variable}to get an environment variable after setting$variableto the string between%.