如果在行中找到特定模式,则查找并替换字符串

如果在行中找到特定模式,则查找并替换字符串

CCSID我需要做的就是,在这个文件中搜索,无论它找到什么CCSIDCHAR该行中的 应该替换为NCHAR,并且VARCHAR该行中的 应该替换为NVARCHAR2

我尝试使用 sed 和 awk。但我找不到解决这个问题的完美方法。

 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 ) )
            ;

输出应该是这样的:

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 ) )
                ;

如何解决这个问题?

答案1

sed

sed '/CCSID/ { s/ CHAR(/ NCHAR(/; s/ VARCHAR(/ NVARCHAR2(/ }' file
  • 第一个模式搜索包含 的行CCSID。然后里面的部分{...}就生效了。
    • s/ CHAR(/ NCHAR(/;CHAR(将(带有前导空格) 替换为NCHAR(.
    • s/ VARCHAR(/ NVARCHAR2(/并替换VARCHAR(NVARCHAR2(.

答案2

perl -pe '/CCSID/ and s/\bCHAR\b/N$&/ || s/\bVARCHAR\b/N$&2(/' file 

相关内容