문제

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