【发布时间】:2014-07-26 15:51:27
【问题描述】:
您好,我需要一次执行多个 sed 操作,然后将输出刷新到文件中。 我有一个 .dat 文件,其数据如下
indicator.dat
Air_Ind - A.Air_Ind Air_Ind - 0000 - 00- 00 Rpting_Ind - Dstbr_Id 为 Null 然后为 'N' 否则为 'Y' End Rpting_Ind - 0000 - 00 - 00 的情况 纬度,经度 - A.Store_Latitude 纬度,A.Store_Longitude 经度 - 0000- 00- 00 合并(Pm_Cig_Direct_Ind,'')-合并(Direct_Acct_Ind,'')- 0004 - 01- 01 合并(Pm_Mst_Direct_Ind,'')-合并(Direct_Acct_Ind,'')- 0004 - 02 - 02 合并(Pm_Snus_Direct_Ind,'')-合并(Direct_Acct_Ind,'')- 0004 - 01 - 02 合并(Pm_Snuf_Direct_Ind,'')-合并(Direct_Acct_Ind,'')- 0004 - 04- 02 合并(Jmc_Cgr_Direct_Ind,'')-合并(Direct_Acct_Ind,'')- 2000 - 02 - 01 合并(Usst_Mst_Direct_Ind,'')-合并(Direct_Acct_Ind,'')- 1070- 02- 02 合并(Usst_Snus_Direct_Ind,'') - 合并(Direct_Acct_Ind,'') - 1070 - 01 - 02 合并(Usst_Snuf_Direct_Ind,'') - 合并(Direct_Acct_Ind,'') - 1070 - 04 - 02
现在我正在尝试替换名为indicator.sql 的文件中定义的参数并将输出刷新到文件中
indicator_s.sql:
Select A.Location_Id,
param1
From Edw.Location A
--1Left Outer Join
--1(
--1 Select
--1 Location_Id,
--1 Direct_Acct_Ind
--1 From Edw.Location_Bcbc D
--1 Where Company_Cd = 'param2'
--1 And Prod_Type_Cd = 'param3'
--1 And Prod_Catg_Cd = 'param4'
--1 ) A
--1 On L.Location_Id = A.Location_Id
Inner Join
Mim.Mdm_Xref_Distributor D
On D.Src_Dstbr_Id=A.Location_Id
Where Sdw_Exclude_Ind='N' And Dstrb_Cd='Us'
任何时候都不会进入else块
#!/bin/sh
rm ./Source_tmp.sql
touch ./Source_tmp.sql
while read line
do
MIM=`echo $line | cut -d " " -f1 `
EDW=`echo $line | cut -d "-" -f2 `
Company_Cd=`echo $line | cut -d "-" -f3 `
Prod_Type_Cd=`echo $line | cut -d "-" -f4 `
Prod_Catg_Cd=`echo $line | cut -d "-" -f5 `
echo "Select top 10 * from (" >> ./Source_tmp.sql ;
sed "s/Param1/$MIM/g" indicator.sql >> Source_tmp.sql;
echo "minus">> Source_tmp.sql;
if [ "$MIM"="Air_Ind " ] || [ "$MIM"="Rpting_Ind " ] || [ "$MIM"="Latitude,Longitude " ]
then
sed "s/param1/$EDW/g" indicator_s.sql >> Source_tmp.sql
else
sed -e "s/--1/' '/g" -e "s/param1/$EDW/g" -e "s/param2/$Company_Cd/g" -e "s/param3/$Prod_Type_Cd/g" -e "s/param4/$Prod_Catg_Cd/g" ./indicator_s.sql >> ./Source_tmp.sql
fi
done <indicator.dat
输出应该像我定义的 param1 和 param2 等值应该从指标 .dat 文件中替换,并且注释行需要在 else 块中取消注释 请帮助我
【问题讨论】:
-
我读到:“我不知道为什么 sed 不能在 ksh 的 else 块中工作” 但在你的脚本中,shebang 是
#!/bin/sh。请修正您的问题/示例/标签,以便澄清您是否需要bash、sh或ksh答案。 -
UNIX shell 是一个调用工具的环境。操作文本文件内容的标准 UNIX 工具是 awk。您应该编写一个 awk 脚本来完成所有操作,然后从您喜欢的任何 shell 中调用它。结果将比你可以用 shell、sed、cut 和其他命令的混合物来做的任何东西都更简洁和健壮。只需清楚地发布 1) indicator.dat、2) indicator.sql 和 3) 预期工具输出的示例,我们就可以为您提供帮助。我保证你会比你现在尝试做的更快乐。
-
indicator.sql中每个数据行之间真的有空行吗? -
no Ed 之间没有空行。