1

So I have a file like

select * from tb where start_date = to_date('20131010','yyyymmdd');
    p23 VARCHAR2(300):='something something
    still part of something above with 'this' between single quotes and close
    something to end';
 (code goes on)

That would be some automatically generated code which I should be able execute via sqlplus. But that obviously won't work, since the third line should have had its quotes escaped like (..) with ''this'' between (...).

I can't access the script that generated that code, but I was trying to get a awk to do the job. Notice the script has to be smart enough to not scape every quote in the code (the to_date('20131010','yyyymmdd') is correct).

I am no expert in awk, so I went as far as:

BEGIN {
    RS=";"
    FS="\n"
}
/\tp[0-9]+/{
    ini = match($0, "\tp[0-9]+")
    fim = match($0, ":='")
    s = substr($0,ini,fim+1)
    txt = substr($0, fim+3, length($0))
    block = substr(txt, 0, length(txt)-1)
    print gensub("'", "''", block)
}
!/\tp[0-9]+/{
    print $0";"
}

but it went way too messy with the print gensub("'", "''", block) and it is not working.

Can someone give me a quick way out?

4

1 回答 1

1

您忘记了 的一个参数gensub。尝试:

BEGIN {
    RS=";"
    FS="\n"
}
/^[[:space:]]+p[0-9]+/{
    ini = match($0, "\tp[0-9]+")
    fim = match($0, ":='")
    s = substr($0,ini,fim+1)
    txt = substr($0, fim+3, length($0))
    block = substr(txt, 0, length(txt)-1)
    printf "%s'%s';", s, gensub("'", "''", "g",block)
    next
}
{
    printf "%s;", $0
}
于 2013-10-31T00:55:12.483 回答