문제

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?

도움이 되었습니까?

해결책

You have forgotten one parameter to gensub. Try:

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
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top