无法一次执行多个sed操作

时间:2014-07-26 15:51:28

标签: sed ksh

我需要一次执行多个sed操作,然后将输出刷新到文件。 我有一个.dat文件,其数据如下

indicator.dat

Air_Ind - A.Air_Ind Air_Ind - 0000 - 00- 00 Rpting_Ind - 当Dstbr_Id为空时的情况然后'N'否则'Y'结束Rpting_Ind - 0000 - 00 - 00 纬度,经度 - A.Store_Latitude纬度,A.Store_Longitude经度--00-0000-00 coalesce(Pm_Cig_Direct_Ind,'') - Coalesce(Direct_Acct_Ind,'') - 0004 - 01-01 coalesce(Pm_Mst_Direct_Ind,'') - Coalesce(Direct_Acct_Ind,'') - 0004 - 02 - 02 coalesce(Pm_Snus_Direct_Ind,'') - Coalesce(Direct_Acct_Ind,'') - 0004 - 01 - 02 coalesce(Pm_Snuf_Direct_Ind,'') - Coalesce(Direct_Acct_Ind,'') - 0004 - 04-02 coalesce(Jmc_Cgr_Direct_Ind,'') - Coalesce(Direct_Acct_Ind,'') - 2000 - 02 - 01 coalesce(Usst_Mst_Direct_Ind,'') - Coalesce(Direct_Acct_Ind,'') - 1070-02-02 coalesce(Usst_Snus_Direct_Ind,'') - Coalesce(Direct_Acct_Ind,'') - 1070 - 01 - 02 coalesce(Usst_Snuf_Direct_Ind,'') - Coalesce(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块中取消注释 请帮助我

2 个答案:

答案 0 :(得分:0)

据我所知,sed命令&#34;正在运行&#34;。但它不会产生预期的产量。我猜。给出一些输出示例:

[...]
' ' Where Company_Cd = ' 0000 '

' ' And Prod_Type_Cd = ' 00 '

' ' And Prod_Catg_Cd = ' 00'
[...]

显然,你在行的开头有额外的引号,并且在你的值附近有额外的间距。

这会解决您的问题:

#!/bin/bash

[...]

MIM=`echo -n $line | cut -d " " -f1 `
EDW=`echo -n $line | cut -d "-" -f2 `
Company_Cd=`echo -n $line | cut -d "-" -f3 `
Prod_Type_Cd=`echo -n $line | cut -d "-" -f4 `
Prod_Catg_Cd=`echo -n $line | cut -d "-" -f5 `

# Trim spaces as you "cut" on "-", keeping extra spaces arround
shopt -s extglob
EDW=${EDW%%+([[:space:]])}; EDW=${EDW##+([[:space:]])};
Company_Cd=${Company_Cd%%+([[:space:]])}; Company_Cd=${Company_Cd##+([[:space:]])};
Prod_Type_Cd=${Prod_Type_Cd%%+([[:space:]])}; Prod_Type_Cd=${Prod_Type_Cd##+([[:space:]])};
Prod_Catg_Cd=${Prod_Catg_Cd%%+([[:space:]])}; Prod_Catg_Cd=${Prod_Catg_Cd##+([[:space:]])};

[...]

# Fix "sed" in your "else" clause by removing extra single quotes
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

现在生成更有效的SQL:

  [...]
  Where Company_Cd = '0000'

  And Prod_Type_Cd = '00'

  And Prod_Catg_Cd = '00'
  [...]

话虽这么说,这主要是一些 hacks 来修复(部分?)你脚本中可能遇到的各种问题。但整个事情似乎有点做作。并且脆弱。例如,如果任何替换字符串包含&,它将会中断。这是Be Dragons。

答案 1 :(得分:0)

到目前为止,我们无法分辨出您希望脚本执行的操作,您只发布了一个脚本,该脚本无法生成您想要的任何输出,并且您还没有发布您想要的输出,但让我们从此开始,您可以更新您的问题以显示预期的输出并澄清您的要求:

$ cat tst.awk
BEGIN{ FS="-" }
NR==FNR { template = (template ? template ORS : "") $0; next }
{
    split($0,arr,/ /)
    MIM = arr[1]
    EDW = $1
    Company_Cd = $3
    Prod_Type_Cd = $4
    Prod_Catg_Cd = $5

    $0 = "Select top 10 * from (\n" template

    gsub(/Param1/,MIM "\nminus")
    gsub(/param1/,EDW)

    if ( MIM !~ /^Air_Ind|Rpting_Ind|Latitude,Longitude/ ) {
        gsub(/--1/," ")
        gsub(/param2/,Company_Cd)
        gsub(/param3/,Prod_Type_Cd)
        gsub(/param4/,Prod_Catg_Cd)
    }

    print
}

$ awk -f tst.awk indicator.sql indicator.dat
Select top 10 * from (
Select A.Id,

 Air_Ind
minus

From Location A

--1Left Outer  Join

--1(

--1 Select

--1 Location_Id,

--1 Direct_Acct_Ind

--1 From Location_Bcbc D

--1 Where Company_Cd = 'param2'

--1 And Prod_Type_Cd = 'param3'

--1 And Prod_Catg_Cd = 'param4'

--1 ) A
Select top 10 * from (
Select A.Id,

 Rpting_Ind
minus

From Location A

--1Left Outer  Join

--1(

--1 Select

--1 Location_Id,

--1 Direct_Acct_Ind

--1 From Location_Bcbc D

--1 Where Company_Cd = 'param2'

--1 And Prod_Type_Cd = 'param3'

--1 And Prod_Catg_Cd = 'param4'

--1 ) A