【问题标题】:selecting required sql from script using shell scripting使用 shell 脚本从脚本中选择所需的 sql
【发布时间】:2021-09-06 20:48:36
【问题描述】:

我有一个 sybase ddl 文件,其中包含许多表的 ddl。但问题在于 ddl 有许多其他东西以及不必要的索引创建脚本和 cmets。有没有办法从这个文件中只提取 ddl。下面是一个小文件示例。

-------------------------------------------
-- DDL fpr table 'prod.dbo.tbl1'
--------------------------------------------
print '<<<< creating table prod.dbo.tbl1
go
setuser 'dbo'
go

create table tbl1 (id numeric (10,0),
                     etity     CHAR(2))

-------------------------------------------
-- DDL fpr table 'prod.dbo.tbl2'
--------------------------------------------
print '<<<< creating table prod.dbo.tbl1
go
setuser 'dbo'
go

create table tbl2 (id numeric (10,0),
                     etity     CHAR(2))
---------------------------------------------------

从这个文件中,我只想要创建表 ddl 的。有什么方法可以实现它..输出应该只是DDL,如下所示..

create table tbl1 id numeric (10,0),
                     etity     CHAR(2);
create table tbl2 id numeric (10,0),
                     etity     CHAR(2);

【问题讨论】:

  • 在所需的输出中,列列表周围缺少括号;你真的要删除那些括号吗?

标签: linux shell unix scripting


【解决方案1】:

假设:

  • 只对create table 命令感兴趣(即,对其他与表相关的DDL 不感兴趣,例如create index、RI/检查约束、默认/规则绑定、缓存配置、grant/revoke 等)
  • create table 块通过空行(无空格)、go(从第 1 列开始)或注释块(-- 从第 1 列开始)终止

示例输入文件:

$ cat ddl.sql
-------------------------------------------
-- DDL fpr table 'prod.dbo.tbl1'
--------------------------------------------
print '<<<< creating table prod.dbo.tbl1
go
setuser 'dbo'
go

create table tbl1 (id numeric (10,0),
                     etity     CHAR(2))

-------------------------------------------
-- DDL fpr table 'prod.dbo.tbl2'
--------------------------------------------
print '<<<< creating table prod.dbo.tbl1
go
setuser 'dbo'
go

create table tbl2 (id numeric (10,0),
                     etity     CHAR(2))
---------------------------------------------------

create table tbl3 (id numeric (10,0),
                     etity     CHAR(2))
go
---------------------------------------------------

一个awk解决方案:

awk '
/create table/         { printme=1 }     # set our "print" flag when we see "create table"
/^$/ || /^go/ || /^--/ { if (printme)    # if in midst of printing "create table" then 
                            print ";"    # print ending ";" and then
                         printme=0       # clear "print" flag
                       }
printme' ddl.sql                         # if "print" flag is set (ie, "=1") then print current line

或者作为删除了 cmets 的单线:

awk '/create table/ {printme=1} /^$/ || /^go/ || /^--/ { if (printme) print ";";printme=0} printme' ddl.sql

对我的ddl.sql 运行上述代码会生成:

create table tbl1 (id numeric (10,0),
                     etity     CHAR(2))
;
create table tbl2 (id numeric (10,0),
                     etity     CHAR(2))
;
create table tbl3 (id numeric (10,0),
                     etity     CHAR(2))
;

注意:如果 OP 需要在行尾以 ; 结尾,则可以在 awk 解决方案中添加更多代码...

【讨论】:

    【解决方案2】:

    如果所有不需要的行都以此字符开头:--、print、go、setuser、...

    你可以这样做:

    cat DDL.txt | egrep -v '^--|^print|^go|^setuser'

    【讨论】:

    • 很少有 aprint 命令和许多不需要的 cmets.. 我只想要从创建表到列末尾的 ddl
    猜你喜欢
    • 1970-01-01
    • 2013-07-16
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 2021-01-21
    • 2015-07-22
    • 1970-01-01
    相关资源
    最近更新 更多