如何从文件中获取详细信息并存储在shell脚本中的临时文件中?

时间:2013-08-20 08:42:56

标签: shell

我有config.txt,其中包含

等详细信息
DB_IP=10.10.1.120, DB_NAME=mysql

然后我创建了一个带有占位符的文件details.txt,即

DB_ip_address=@@ip@@, DB_name=@@name@@

现在我需要一个临时文件,从config.txt获取详细信息并替换details.txt中的占位符。所以我的临时文件应该是

DB_ip_address=10.10.1.120 
DB_name=mysql

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

x=($(grep -Po "(?<=DB_IP=)(.+)(?=,)|(?<=DB_NAME=)(.+)" config.txt))  
y=($(grep -Eo "[a-zA-Z_]+=" details.txt))
echo $y$x > temp.txt
echo ${y[1]}${x[1]} >> temp.txt

假设两个文件中DB_IP和DB_NAME的顺序始终相同。