在我的文件中,有很多'更新' 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 ';
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 ';
答案 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