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'
有帮助吗?

解决方案

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.

其他提示

Using sed

export SOME_ENV_VARIABLE=twochain;
sed -i "s/@[^@]*@/${SOME_ENV_VARIABLE}/" FILE1 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top