【问题标题】:Grep / Find oracle table name from UNIX Shell / SQL ScriptGrep / 从 UNIX Shell / SQL 脚本中查找 oracle 表名
【发布时间】:2013-12-24 03:26:27
【问题描述】:

这是 (*.sql) 文件中的示例更新脚本。我必须从下面显示的输出中的 shell 脚本/sql 脚本中提取 oracle 表名。另外我附上下面的源代码。我试图通过 awk 命令来完成,

代码:

awk -F'[=&? ]' '{for (i=1;i<NF;i++) if ($i == "from") print (i+1) }' $FILE

输出:

UPDATE|EVENT.SEASONAL_EVENT_SKU
SELECT|PLANOGRAM_SKU
SELECT|PLANOGRAM_VERSION
SELECT|SEASONAL_EVENT_SKU

示例脚本:

update EVENT.SEASONAL_EVENT_SKU
set SEAS_SKU_IND = 'Y'
where SKU_NBR in ( select distinct sku_nbr
               from
             (
               select a.sku_nbr
               from PLANOGRAM_SKU a,
                    PLANOGRAM_VERSION b
               where a.plano_nbr between 9000 and 9999
               and   a.plano_nbr        = b.plano_nbr
               and   a.plano_version_id = b.plano_version_id
               and   b.seas_plano_ind   = 'Y'
               and   a.sku_nbr not in ( select d.sku_nbr
                                        from PLANOGRAM_SKU d,
                                             PLANOGRAM_VERSION e
                                        where d.plano_nbr between 9000 and 9999
                                        and   d.plano_nbr        = e.plano_nbr
                                        and   d.plano_version_id = e.plano_version_id
                                        and   e.seas_plano_ind <> 'Y')
               union all
               select f.sku_nbr
               from SEASONAL_EVENT_SKU f
               where f.sku_nbr not in ( select g.sku_nbr
                                        from PLANOGRAM_SKU g
                                        where g.plano_nbr between 9000 and 9999 )));

COMMIT;

让我知道对“n”个脚本执行此操作的更好方法。谢谢。

【问题讨论】:

    标签: unix sed awk grep find


    【解决方案1】:

    使用 GNU awk 将 RS 设置为 RE:

    $ cat tst.awk
    BEGIN{ RS="[[:space:]]+"; OFS="|" }
    /^(update|set|from|[[:upper:]._]+)$/ {
        if (/^[[:upper:]._]+$/) {
            if (op != "SET") {
                if (!seen[op,$0]++) {
                    print op, $0
                }
            }
        }
        else {
            op = (/^from/ ? "SELECT" : toupper($0))
        }
    }
    $ gawk -f tst.awk file
    UPDATE|EVENT.SEASONAL_EVENT_SKU
    SELECT|PLANOGRAM_SKU
    SELECT|PLANOGRAM_VERSION
    SELECT|SEASONAL_EVENT_SKU
    

    对于其他 awk,只需将 RS 保留为默认值并在字段上使用循环。

    【讨论】:

    • 如何增强其他 dml (insert,delete,merge) , ddl (create,drop)。
    • 取决于它们在您的输入文件中的样子。如果没有看到使用它们的输入文件,很难猜测什么会起作用,但将它们添加到 |-分隔列表 /^(update|set|from|[[:upper:]._]+)$/ 开始,然后从那里开始。
    猜你喜欢
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    • 2018-02-27
    • 2018-02-26
    • 2012-06-30
    • 2013-08-14
    • 1970-01-01
    相关资源
    最近更新 更多