Question

I would like to know what shell command would allow me to replace the tokens e.g. myproperty='@SOME_ENV_VARIABLE@' defined in a file with their corresponding environment variables in unix.

i.e.

cat FILE1 ;
someproperty='@SOME_ENV_VARIABLE@'

export SOME_ENV_VARIABLE=twochain;
...do the replace here with FILE1...

cat FILE1 ;
someproperty='twochain'
Was it helpful?

Solution

perl -pe 's/\@(.*?)\@/$ENV{$1}/g' < infile > outfile

EDIT: To modify a particular file in-place, creating a backup of the original in somefile.bak:

perl -i.bak -pe 's/\@(.*?)\@/$ENV{$1}/g' somefile

Delete the .bak characters to avoid creating any backup file.

OTHER TIPS

Using sed

export SOME_ENV_VARIABLE=twochain;
sed -i "s/@[^@]*@/${SOME_ENV_VARIABLE}/" FILE1 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top