将单个 sql 文件拆分为多个文件
Split a single sql file into multiple files
我有一个文件 master.sql 包含许多创建的 table ddl。
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
) ;
我想将此文件拆分成单独的文件 - 每个文件一个 table。为此,我正在使用 rubin's solution here.
这是我使用的 awk 命令。
awk '/CREATE TABLE/{f=0 ;n++; print >(file=n); close(n-1)} f{ print > file}; /CREATE TABLE/{f=1}' master.sql
在执行 awk 命令时生成 table 个没有任何扩展名的文件。尝试使用此 article
联系
创建每个 sql 文件时,我想更改 table 名称的文件名。
例如
customers.sql
inventory.sql
我正在尝试使用 awk 命令获取 table 名称形式 master.sql。是否有可能在迭代 master.sql.
时获得 table 名称
有办法解决这个问题吗?
这是一个简单的两步过程:
# 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
) ;
你好,你可以使用类似的东西:
awk 'BEGIN{RS=";"} /CREATE TABLE/{fn = ".sql"; print [=10=] ";" > fn; close(fn);}' master.sql
BEGIN
块将使用 ;
字符作为记录分隔符将输入拆分为 sql 语句(而不是行)。
如果该行匹配 CREATE TABLE
到基于第三个字段(table 名称)
的文件名,则可以打印语句内容
注意:如果有任何 sql 评论包含 ;
,这可能不会很好用
已编辑以关闭文件(请参阅@ed-morton 的评论)
你使用的那个 awk 命令对于你正在做的事情来说复杂得离谱。它所需要的只是:
awk '/CREATE TABLE/{close(n); n++} {print > n}' file
对于您的新要求,它只是一个调整:
$ awk '/CREATE TABLE/{close(out); out= ".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
) ;
我有一个文件 master.sql 包含许多创建的 table ddl。
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
) ;
我想将此文件拆分成单独的文件 - 每个文件一个 table。为此,我正在使用 rubin's solution here.
这是我使用的 awk 命令。
awk '/CREATE TABLE/{f=0 ;n++; print >(file=n); close(n-1)} f{ print > file}; /CREATE TABLE/{f=1}' master.sql
在执行 awk 命令时生成 table 个没有任何扩展名的文件。尝试使用此 article
联系创建每个 sql 文件时,我想更改 table 名称的文件名。
例如
customers.sql
inventory.sql
我正在尝试使用 awk 命令获取 table 名称形式 master.sql。是否有可能在迭代 master.sql.
时获得 table 名称有办法解决这个问题吗?
这是一个简单的两步过程:
# 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
) ;
你好,你可以使用类似的东西:
awk 'BEGIN{RS=";"} /CREATE TABLE/{fn = ".sql"; print [=10=] ";" > fn; close(fn);}' master.sql
BEGIN
块将使用 ;
字符作为记录分隔符将输入拆分为 sql 语句(而不是行)。
如果该行匹配 CREATE TABLE
到基于第三个字段(table 名称)
注意:如果有任何 sql 评论包含 ;
已编辑以关闭文件(请参阅@ed-morton 的评论)
你使用的那个 awk 命令对于你正在做的事情来说复杂得离谱。它所需要的只是:
awk '/CREATE TABLE/{close(n); n++} {print > n}' file
对于您的新要求,它只是一个调整:
$ awk '/CREATE TABLE/{close(out); out= ".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
) ;