【问题标题】:Awk to scape quotation marksawk 转义引号
【发布时间】:2013-10-30 22:57:49
【问题描述】:

所以我有一个类似的文件

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)

这将是一些自动生成的代码,我应该可以通过 sqlplus 执行这些代码。但这显然行不通,因为第三行的引号应该像(..) with ''this'' between (...) 一样被转义。

我无法访问生成该代码的脚本,但我试图让 awk 来完成这项工作。请注意,脚本必须足够聪明,才能不转义代码中的每个引号(to_date('20131010','yyyymmdd') 是正确的)。

我不是 awk 专家,所以我做到了:

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";"
}

但是print gensub("'", "''", block) 太乱了,它不起作用。

谁能给我一个快速的出路?

【问题讨论】:

    标签: awk quotes


    【解决方案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
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-27
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 2011-12-09
      • 2013-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多