cat inputfile | tr '\n' '@' | sed -e 's/;/;\n/g' | grep UNIQUE | tr '@' '\n'
Eu tenho um arquivo com o conteúdo abaixo. Tudo o que preciso fazer é filtrar o script ALTER que tem uma palavra-chave UNIQUE e movê-lo para um arquivo diferente. Eu tentei usar sed -n '/ALTER TABLE/,/UNIQUE("ID");/p' file
. Mas não funcionou.
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 ) ) ;
A saída deve ser como o conteúdo abaixo em um arquivo diferente.
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 ) ;
Você pode fazer isso apenas por sed
sed ':1;/ALTER/N;/;/!b1;/UNIQUE/!d' inputfile > outputfile
ou mesmo com uso de espaço padrão
sed 'H;/ALTER/h;/;/x;/UNIQUE/!d' inputfile > outputfile
Ou você pode usar awk
pelo mesmo estilo:
awk 'b{b=b "\n" $0}/ALTER/{b=$0}/;/{if(b~/UNIQUE/)print b;b=""}' inputfile
Tags awk sed shell-script