选择内容并将其移动到另一个文件

选择内容并将其移动到另一个文件

我有一个包含以下内容的文件。我需要做的就是过滤改变脚本有独特的其中的关键字并将其移动到另一个文件。我尝试使用sed -n '/ALTER TABLE/,/UNIQUE("ID");/p' file.但这没有用。

ALTER TABLE RELEASE.ACL_ENTRY
            ADD CONSTRAINT RELEASE.FK5111A
            FOREIGN KEY( ACL_OID )
            REFERENCES RELEASE.ACL ( OID )
            ON DELETE CASCADE
            --ON UPDATE NO ACTION ;
    ALTER TABLE RELEASE.ACTION_DESC
            ADD CONSTRAINT RELEASE.UN2160A
            UNIQUE( ID_MASK ) ;
    ALTER TABLE RELEASE.ACTION_DESC
            ADD CONSTRAINT RELEASE.UN2160B
            UNIQUE( NAME ) ;
    ALTER TABLE RELEASE.ACTION_DESC_LOD
            ADD CONSTRAINT RELEASE.FK2170
            FOREIGN KEY( ACTION_DESC_OID )
            REFERENCES RELEASE.ACTION_DESC ( OID )
            ON DELETE CASCADE
            --ON UPDATE NO ACTION ;
    ALTER TABLE RELEASE.ACTION_SET
            ADD CONSTRAINT RELEASE.UN2120A
            UNIQUE( "ID" ) ;
    ALTER TABLE RELEASE.ACTION_SET
            ADD CONSTRAINT RELEASE.UN2120B
            UNIQUE( NAME ) ;
    ALTER TABLE RELEASE.ACTION_SET_LOD
            ADD CONSTRAINT RELEASE.FK2150A
            FOREIGN KEY( ACTION_SET_OID )
            REFERENCES RELEASE.ACTION_SET ( OID )
            ON DELETE CASCADE
            --ON UPDATE NO ACTION ;
    ALTER TABLE RELEASE.APP_DESC
            ADD CONSTRAINT RELEASE.FK20A
            FOREIGN KEY( WEB_MOD_OID )
            REFERENCES RELEASE.WEB_MOD ( OID )
            ON DELETE CASCADE
            --ON UPDATE NO ACTION ;
    ALTER TABLE RELEASE.APP_DESC
            ADD CONSTRAINT RELEASE.CC20B
            CHECK( ( WEB_MOD_OID IS NOT NULL ) AND ( WEB_MOD_SL IS NULL )  ) ;

输出应类似于不同文件中的以下内容。

ALTER TABLE RELEASE.ACTION_DESC
                ADD CONSTRAINT RELEASE.UN2160A
                UNIQUE( ID_MASK ) ;
        ALTER TABLE RELEASE.ACTION_DESC
                ADD CONSTRAINT RELEASE.UN2160B
                UNIQUE( NAME ) ;
ALTER TABLE RELEASE.ACTION_SET
                ADD CONSTRAINT RELEASE.UN2120A
                UNIQUE( "ID" ) ;
        ALTER TABLE RELEASE.ACTION_SET
                ADD CONSTRAINT RELEASE.UN2120B
                UNIQUE( NAME ) ;

答案1

cat inputfile | tr '\n' '@' | sed -e 's/;/;\n/g' | grep UNIQUE | tr '@' '\n'

答案2

sed你只能通过

sed ':1;/ALTER/N;/;/!b1;/UNIQUE/!d' inputfile > outputfile

或与模式空间使用相同

sed 'H;/ALTER/h;/;/x;/UNIQUE/!d' inputfile > outputfile

或者您可以使用awk相同的样式:

awk 'b{b=b "\n" $0}/ALTER/{b=$0}/;/{if(b~/UNIQUE/)print b;b=""}' inputfile

相关内容