我在Linux中有两个文件,在文件中有这些变量:
${VERSION} ${SOFTWARE_PRODUCER}
这些变量的值存储在文件b中:
VERSION=1.0.1
SOFTWARE_PRODUCER=Luc
现在我如何使用命令将文件a中的变量替换为文件b中的值?是sed
能够完成这项任务吗?
感谢。
答案 0 :(得分: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
$