0

我有一个输出文件(SQL DDL实际上是一个),我想将某些块写入一个文件,其余块写入第二个文件。
我在想gawk或者sed可以做到这一点。
具体来说,我想基于类似/^CREATE (TABLE|VIEW)[^;]+/一个文件的东西重定向,然后将其余的转储到第二个。

4

1 回答 1

0
sed -n -r -e '/CREATE (VIEW|TABLE)/,/;/ { w file1
              d
              }
              w file2' file.sql

这假定与 CREATE 语句关联的分号与 CREATE 关键字不在同一行。

文件.sql

CREATE VIEW xyz AS
    SELECT * FROM SomeTable;

GRANT ALL ON xyz TO public;

CREATE TABLE pqr
(
    abc INTEGER NOT NULL,
    def VARCHAR(255) NOT NULL
);

GRANT SELECT, INSERT ON pqr TO public;

CREATE VIEW jkl AS
    SELECT * FROM pqr;

文件 1

CREATE VIEW xyz AS
    SELECT * FROM SomeTable;
CREATE TABLE pqr
(
    abc INTEGER NOT NULL,
    def VARCHAR(255) NOT NULL
);
CREATE VIEW jkl AS
    SELECT * FROM pqr;

文件2

GRANT ALL ON xyz TO public;


GRANT SELECT, INSERT ON pqr TO public;
于 2013-05-16T23:50:00.537 回答