如何只将模式字符串打印到模式结束点

时间:2015-07-23 03:38:28

标签: awk

在我的文件中,有很多'更新' SQL语句,我希望获得所需的输出,如问题的底部。 file.awk失败。

你能帮助我做得更好吗?

文件

-- [1] Wed May  7 07:30:05 2014

-- DDDDDDDD DDDD DDDDD
-- KKKKK KKKKK KKKKK KKKKKK
update   "SCOTT"."TABLE" t 
set "KKKK"='01',"BBBBBB"='20140507073000   ' 
where "AAAAA" = 1 
and "BBBBBB"='20140502180500   ';

-- [2] Wed May  7 07:30:05 2014

-- FFFFFFF DDDD DDDDD
-- KKKKK KKKKK KKKKK KKKKKK
update   "SCOTT"."TABLE2" t 
set "KKKK"='01',"BBBBBB"='20140507073000   ' 
where "AAAAA" = 5 
and "BBBBBB"='20160502180500   ';

awk脚本(file.awk)

BEGIN {
      matched = 0;
}
/] /,/;/ {                       
           if (/update:$/) {
                         splitHipen=$2;
                         split(splitHipen,splitHipenArr,".");
                         printf "%s#",substr(splitHipenArr[1],2,length(splitHipenArr[1])-2);   #TABLE_OWNER
                         printf "%s#",substr(splitHipenArr[2],2,length(splitHipenArr[2])-2);   #TABLE_NAME

              { matched = 1 } matched { print }

             }

      } 

END {
}   

期望输出

SCOTT#TABLE#update   "SCOTT"."TABLE" t 
    set "KKKK"='01',"BBBBBB"='20140507073000   ' 
    where "AAAAA" = 1 
    and "BBBBBB"='20140502180500   ';   
SCOTT#TABLE2#update   "SCOTT"."TABLE2" t 
    set "KKKK"='01',"BBBBBB"='20140507073000   ' 
    where "AAAAA" = 5 
    and "BBBBBB"='20160502180500   ';   

3 个答案:

答案 0 :(得分:0)



'BEGIN{RS=";"}{sub(".*update","update")}{if (/update/){
                         splitHipen=$2;
                         split(splitHipen,splitHipenArr,".");
                         printf "%s#",substr(splitHipenArr[1],2,length(splitHipenArr[1])-2);   #TABLE_OWNER
                         printf "%s#",substr(splitHipenArr[2],2,length(splitHipenArr[2])-2);   #TABLE_NAME

              printf("%s;\n",$0)

             }}'




答案 1 :(得分:0)

/^--/ {next;}  # Skip comments
/update/ { schema=$2""$1;
           gsub(/[". ]+/, "#", schema);
           print substr(schema, 2), $2, $3;
           next} # Re-arrange fields
1  # Default action, just print

答案 2 :(得分:0)

更新Ramana的回答。

/^-- / && !/]/{next}  # Skip comments which doesn't contain ]
/]/{schema=" "$3;next} # Store Day in variable schema
/update/ { schema=schema""$2""$1;
           gsub(/[". ]+/, "#", schema);
           print substr(schema, 2), $2, $3;
           next} # Re-arrange fields
1