2016-03-17 47 views
2

我有兩個文件在Linux中,在文件中有這樣的變量:Unix的:在文件B與值替換文件中的變量

${VERSION} ${SOFTWARE_PRODUCER} 

而這些變量的值存儲在文件B:

VERSION=1.0.1 
SOFTWARE_PRODUCER=Luc 

現在,我怎麼使用命令,在文件b值替換文件中的變量?像sed能夠完成這項任務嗎? 謝謝。

回答

0

一個簡單的bash循環就足夠了:

$ cat a 
This is file 'a' which has this variable ${VERSION} 
and it has this also: 
${SOFTWARE_PRODUCER} 
$ cat b 
VERSION=1.0.1 
SOFTWARE_PRODUCER=Luc 
$ cat script.bash 
#!/bin/bash 
while read line || [[ -n "$line" ]] 
do 
    key=$(awk -F= '{print $1}' <<< "$line") 
    value=$(awk -F= '{print $2}' <<< "$line") 
    sed -i 's/${'"$key"'}/'"$value"'/g' a 
done < b 
$ ./script.bash 
$ cat a 
This is file 'a' which has this variable 1.0.1 
and it has this also: 
Luc 
$ 
+0

謝謝你這是真棒 – brest1007