从文件中读取后,无法在Unix中分配值

时间:2013-06-20 13:18:56

标签: unix sed

我正在使用Cygwin并打算在Unix中使用相同的脚本。我有一个文件done.txt,其中包含34523的值。我想用?替换CreateView.sql文件中的每个34523,并将输出写入CreateViewFinal.sql文件。但它没有完成预期的工作,而是直接在$value文件中分配CreateViewFinal.sql。在这件事上的任何帮助都会很大。找到脚本:

脚本:

sed '14 ! d' result.txt >> done.txt
value=$(<done.txt)
echo "$value"
sed 's/?/$value/' CreateView.sql >> CreateViewFinal.sql

完成所有工作后我将CreateViewFinal.sql作为

create view Latest_Cust_Records_Final  as
select cust.* from MCT cust,goldencopy g,principalkey p
where g.catalogid=$value and cust.cproductkeyid=g.productkey  and cust.cmodversion=g.version 
and p.catalogid=$value and p.active='Y' and cust.cproductkeyid=p.productkeyid;

所以我希望$value来自done.txt并分配到CreateViewFinal.txt。欲望结果应该看起来像

create view Latest_Cust_Records_Final  as
select cust.* from MCT cust,goldencopy g,principalkey p
where g.catalogid=34523 and cust.cproductkeyid=g.productkey  and cust.cmodversion=g.version 
and p.catalogid=34523 and p.active='Y' and cust.cproductkeyid=p.productkeyid;

1 个答案:

答案 0 :(得分:1)

在sed调用中使用双引号而不是单引号:

sed "s/?/$value/" CreateView.sql >> CreateViewFinal.sql

使用单引号时,$value等shell变量引用不会被替换。