【问题标题】:Split a single sql file into multiple files将单个sql文件拆分为多个文件
【发布时间】:2022-01-10 03:06:05
【问题描述】:

我有一个包含许多创建表 ddl 的文件 master.sql。

master.sql

CREATE TABLE customers (
    customer_id numeric(38) GENERATED BY DEFAULT AS IDENTITY,
    email_address varchar(255) NOT NULL,
    full_name varchar(255) NOT NULL
) ;

CREATE TABLE inventory (
    inventory_id numeric(38) GENERATED BY DEFAULT AS IDENTITY,
    store_id numeric(38) NOT NULL,
    product_id numeric(38) NOT NULL,
    product_inventory numeric(38) NOT NULL
) ;

我想将此文件拆分为单独的文件 - 每个文件用于一个表。为此,我使用rubin's 解决方案here

这是我使用的 awk 命令。

awk '/CREATE TABLE/{f=0 ;n++; print >(file=n); close(n-1)} f{ print > file}; /CREATE TABLE/{f=1}'  master.sql

在执行 awk 命令时,会生成带有表数且没有任何扩展名的文件。尝试使用此article联系

创建每个 sql 文件时,我想将文件名更改为表名。

例如。

  • customers.sql

  • inventory.sql

我正在尝试使用 awk 命令从 master.sql 中获取表名。迭代master.sql时是否可以获取表名。

有没有办法解决这个问题?

【问题讨论】:

    标签: regex shell awk script


    【解决方案1】:

    你好,你可以使用类似的东西:

    awk 'BEGIN{RS=";"} /CREATE TABLE/{fn = $3 ".sql"; print $0 ";" > fn; close(fn);}' master.sql
    

    BEGIN 块将使用 ; 字符作为记录分隔符将输入拆分为 sql 语句(而不是行)。

    如果该行将CREATE TABLE与基于第三个字段(表名)的文件名匹配,则可以打印语句内容

    注意:如果有任何 sql cmets 包含 ;,这可能效果不佳

    编辑关闭文件(参见@ed-morton 的评论)

    【讨论】:

      【解决方案2】:

      你使用的那个 awk 命令对于你正在做的事情来说是非常复杂的。只需要:

      awk '/CREATE TABLE/{close(n); n++} {print > n}' file
      

      对于您的新要求,只需稍作调整即可:

      $ awk '/CREATE TABLE/{close(out); out=$3 ".sql"} {print > out}' file
      

      $ head *.sql
      ==> customers.sql <==
      CREATE TABLE customers (
          customer_id numeric(38) GENERATED BY DEFAULT AS IDENTITY,
          email_address varchar(255) NOT NULL,
          full_name varchar(255) NOT NULL
      ) ;
      
      
      ==> inventory.sql <==
      CREATE TABLE inventory (
          inventory_id numeric(38) GENERATED BY DEFAULT AS IDENTITY,
          store_id numeric(38) NOT NULL,
          product_id numeric(38) NOT NULL,
          product_inventory numeric(38) NOT NULL
      ) ;
      

      【讨论】:

        【解决方案3】:

        这是一个简单的两步过程:

        # Split the files when the string CREATE TABLE is found
        csplit master.sql '/CREATE TABLE/'
        
        # Read the first line, extract table name and rename the file
        for f in $(ls xx*); 
        do 
            table_name=`head -1 $f | awk '{ sub(/.*CREATE TABLE /, ""); sub(/ .*/, ""); print }'`
            mv $f "$table_name.sql"
            echo "Renaming $f to $table_name.sql"; 
        done;
        

        ->

        Renaming xx00 to customers.sql
        Renaming xx01 to inventory.sql
        

        ->

        $ ls
        customers.sql inventory.sql master.sql
        
        $ cat customers.sql
          CREATE TABLE customers (
            customer_id numeric(38) GENERATED BY DEFAULT AS IDENTITY,
            email_address varchar(255) NOT NULL,
            full_name varchar(255) NOT NULL
        ) ;
        
        $ cat inventory.sql
        CREATE TABLE inventory (
            inventory_id numeric(38) GENERATED BY DEFAULT AS IDENTITY,
            store_id numeric(38) NOT NULL,
            product_id numeric(38) NOT NULL,
            product_inventory numeric(38) NOT NULL
        ) ;
        

        【讨论】:

        • 您应该将该脚本复制/粘贴到shellcheck.net 并修复它告诉您的问题。
        猜你喜欢
        • 2013-08-24
        • 1970-01-01
        • 2019-11-26
        • 2014-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-22
        相关资源
        最近更新 更多