Modifying Variables in Files

hi list,

I am currently looking to develop an installation script which writes out .conf files based on existing .conf files according to variables which are set in a settings file.

For example I have a settings file like so:

ip=192.168.1.1
hosts=local

And I want to read a file which looks like this:

[test]
ip_address=$ip
hostname=$hosts
othervalue=110
testfield=testvalue

And create an output file so that it looks like this whilst leaving all other fields in the source file intact except for the variables:

[test]
ip_address=192.168.1.1
hostname=local
othervalue=110
testfield=testvalue

What is the best way to script this?

thanks,
lland

awk -F'=' 'NR==FNR{A[$1]=$2;next}/\$/{sub(/\$/,x);if(A[$2]) $2=A[$2]}1' OFS='=' file.set file.conf
1 Like

This works perfectly. :slight_smile:

So my last question is. What if I want to remove the '=' from the output so that instead of the output being:

ip_address=192.168.1.1
it reads:
ip_address 192.168.1.1
thanks,
lland

Assuming you want to remove all assignment operator from output:

awk -F'=' 'NR==FNR{A[$1]=$2;next}/\$/{sub(/\$/,x);if(A[$2]) $2=A[$2]}{sub("=",OFS)}1' file.set file.conf
1 Like