Encontre e substitua uma string se determinado padrão for encontrado em uma linha

1

Tudo o que preciso fazer é procurar por CCSID nesse arquivo, sempre que encontrar CCSID , o CHAR dessa linha deve ser substituído por NCHAR e VARCHAR nessa linha. com NVARCHAR2 .

Eu tentei usar sed e awk. Mas não consegui encontrar uma maneira perfeita de resolver esse problema.

 CREATE TABLE JCR.ICMSTSYSCONTROL (
            LIBRARYSERVERID  INTEGER NOT NULL ,
            LANGUAGECODE  CHAR(3) CCSID 37 NOT NULL ,
            SYSSEGMENTID  SMALLINT NOT NULL ,
            SYSSEGMENTTHRESHLD  INTEGER NOT NULL ,
            ACLBINDINGLEVEL  SMALLINT NOT NULL ,
            LIBRARYACLCODE  INTEGER NOT NULL ,
            PUBACCESSENABLED  SMALLINT NOT NULL ,
            DFLTACLCHOICE  SMALLINT NOT NULL ,
            SMSCHOICE SMALLINT NOT NULL ,
            TRACELEVEL SMALLINT NOT NULL ,
            MAXUSERS INTEGER NOT NULL ,
            MAXUSERACTION  SMALLINT NOT NULL ,
            CURRENTUSERS  INTEGER NOT NULL ,
            MAXLOGONRETRY  SMALLINT NOT NULL ,
            PASSWORDDURATION  SMALLINT NOT NULL ,
            SYSADMINEVENTFLAG  SMALLINT NOT NULL ,
            SYSTEMFLAG SMALLINT NOT NULL ,
            DATABASETYPE  SMALLINT NOT NULL ,
            MAXTXDURATION  INTEGER NOT NULL ,
            MAXRESULTSETSIZE  INTEGER NOT NULL ,
            ALLOWTRUSTEDLOGON  SMALLINT NOT NULL ,
            DOCROUTINGUPDATE  INTEGER NOT NULL ,
            DOCROUTINGFREQ  SMALLINT NOT NULL ,
            PLATFORM SMALLINT NOT NULL ,
            SYSTIMEOUT SMALLINT NOT NULL ,
            TIEUSERID CHAR(175) CCSID 37 DEFAULT NULL ,
            TIEPASSWORD  CHAR(72) FOR BIT DATA DEFAULT NULL ,
            DATABASENAME  VARCHAR(128) CCSID 37 NOT NULL ,
            DBSCHEMANAME  VARCHAR(128) CCSID 37 NOT NULL ,
            TRACEFILENAME  VARCHAR(128) CCSID 37 DEFAULT NULL ,
            ENCRYPTIONKEY  VARCHAR(128) FOR BIT DATA NOT NULL ,
            KEEPTRACEOPEN  SMALLINT NOT NULL ,
            MULTIPLETRACEFILES  SMALLINT NOT NULL ,
            MAXTRACEFILESIZE  INTEGER NOT NULL ,
            PATHICMROOT  VARCHAR(128) CCSID 37 NOT NULL ,
            PATHICMDLL VARCHAR(128) CCSID 37 NOT NULL ,
            SUSPENDSERVERTIME  TIMESTAMP DEFAULT NULL ,
            RMSTATUSINTERVAL  SMALLINT NOT NULL ,
            RMSTATUSTIMEOUT  SMALLINT NOT NULL ,
            TIEINTERVAL  SMALLINT NOT NULL ,
            LSCURRENTVERSION  VARCHAR(128) CCSID 37 NOT NULL ,
            TRACEUSER CHAR(175) CCSID 37 DEFAULT NULL ,
            DIMSGDIGESTALGO  SMALLINT NOT NULL DEFAULT 0 ,
            DIENCRYPTIONALGO  SMALLINT NOT NULL DEFAULT 0 ,
            CONSTRAINT JCR.ICMSTSYSCONTROLPK PRIMARY KEY( LIBRARYSERVERID ) )
            ;

A saída deve ser como:

CREATE TABLE JCR.ICMSTSYSCONTROL (
                LIBRARYSERVERID  INTEGER NOT NULL ,
                LANGUAGECODE  NCHAR(3) CCSID 37 NOT NULL ,
                SYSSEGMENTID  SMALLINT NOT NULL ,
                SYSSEGMENTTHRESHLD  INTEGER NOT NULL ,
                ACLBINDINGLEVEL  SMALLINT NOT NULL ,
                LIBRARYACLCODE  INTEGER NOT NULL ,
                PUBACCESSENABLED  SMALLINT NOT NULL ,
                DFLTACLCHOICE  SMALLINT NOT NULL ,
                SMSCHOICE SMALLINT NOT NULL ,
                TRACELEVEL SMALLINT NOT NULL ,
                MAXUSERS INTEGER NOT NULL ,
                MAXUSERACTION  SMALLINT NOT NULL ,
                CURRENTUSERS  INTEGER NOT NULL ,
                MAXLOGONRETRY  SMALLINT NOT NULL ,
                PASSWORDDURATION  SMALLINT NOT NULL ,
                SYSADMINEVENTFLAG  SMALLINT NOT NULL ,
                SYSTEMFLAG SMALLINT NOT NULL ,
                DATABASETYPE  SMALLINT NOT NULL ,
                MAXTXDURATION  INTEGER NOT NULL ,
                MAXRESULTSETSIZE  INTEGER NOT NULL ,
                ALLOWTRUSTEDLOGON  SMALLINT NOT NULL ,
                DOCROUTINGUPDATE  INTEGER NOT NULL ,
                DOCROUTINGFREQ  SMALLINT NOT NULL ,
                PLATFORM SMALLINT NOT NULL ,
                SYSTIMEOUT SMALLINT NOT NULL ,
                TIEUSERID NCHAR(175) CCSID 37 DEFAULT NULL ,
                TIEPASSWORD  CHAR(72) FOR BIT DATA DEFAULT NULL ,
                DATABASENAME  NVARCHAR2(128) CCSID 37 NOT NULL ,
                DBSCHEMANAME  NVARCHAR2(128) CCSID 37 NOT NULL ,
                TRACEFILENAME  NVARCHAR2(128) CCSID 37 DEFAULT NULL ,
                ENCRYPTIONKEY  VARCHAR(128) FOR BIT DATA NOT NULL ,
                KEEPTRACEOPEN  SMALLINT NOT NULL ,
                MULTIPLETRACEFILES  SMALLINT NOT NULL ,
                MAXTRACEFILESIZE  INTEGER NOT NULL ,
                PATHICMROOT  NVARCHAR2(128) CCSID 37 NOT NULL ,
                PATHICMDLL NVARCHAR2(128) CCSID 37 NOT NULL ,
                SUSPENDSERVERTIME  TIMESTAMP DEFAULT NULL ,
                RMSTATUSINTERVAL  SMALLINT NOT NULL ,
                RMSTATUSTIMEOUT  SMALLINT NOT NULL ,
                TIEINTERVAL  SMALLINT NOT NULL ,
                LSCURRENTVERSION  NVARCHAR2(128) CCSID 37 NOT NULL ,
                TRACEUSER NCHAR(175) CCSID 37 DEFAULT NULL ,
                DIMSGDIGESTALGO  SMALLINT NOT NULL DEFAULT 0 ,
                DIENCRYPTIONALGO  SMALLINT NOT NULL DEFAULT 0 ,
                CONSTRAINT JCR.ICMSTSYSCONTROLPK PRIMARY KEY( LIBRARYSERVERID ) )
                ;

Como resolver isso?

    
por sabarish jackson 29.09.2015 / 12:51

2 respostas

3

com sed :

sed '/CCSID/ { s/ CHAR(/ NCHAR(/; s/ VARCHAR(/ NVARCHAR2(/ }' file
  • O primeiro padrão procura por linhas contendo CCSID . Então a parte dentro de {...} entra em vigor.
    • s/ CHAR(/ NCHAR(/; substitui CHAR( (com espaço na liderança) por NCHAR( .
    • s/ VARCHAR(/ NVARCHAR2(/ e substitua VARCHAR( por NVARCHAR2( .
por 29.09.2015 / 13:00
0
perl -pe '/CCSID/ and s/\bCHAR\b/N$&/ || s/\bVARCHAR\b/N$&2(/' file 
    
por 29.09.2015 / 16:39