파일에서 특정 패턴을 찾아 하나씩 새로운 파일로 옮겨보려고 합니다. 시도해봤는데 sed -n 's/CREATE PROCEDURE/,/END ;/p' File
같은 파일이 다시 인쇄됩니다. 아래는 파일 내용입니다.
SET PATH "QSYS","QSYS2","JOES" ;
CREATE PROCEDURE JOES.CANCELORDERLINE (
IN ORDERID INTEGER ,
IN ORDERLINEID INTEGER ,
IN NEWORDLINESTATUSCODE VARCHAR(10) ,
IN NAME VARCHAR(50) ,
OUT O_RESULT CHAR(5) )
LANGUAGE SQL
SPECIFIC JOES.CANCELORDERLINE
NOT DETERMINISTIC
MODIFIES SQL DATA
CALLED ON NULL INPUT
SET OPTION ALWBLK = *ALLREAD ,
ALWCPYDTA = *OPTIMIZE ,
COMMIT = *NONE ,
DECRESULT = (31, 31, 00) ,
DFTRDBCOL = *NONE ,
DYNDFTCOL = *NO ,
DYNUSRPRF = *USER ,
SRTSEQ = *HEX
BEGIN ATOMIC
DECLARE V_ORDLINENUMFORDSP VARCHAR ( 16 ) ;
DECLARE V_ORDLINENUMSTR VARCHAR ( 3 ) ;
DECLARE V_ORDLINENUM INT ;
DECLARE V_RESULT CHAR ( 5 ) DEFAULT '00000' ;
DECLARE SQLSTATE CHAR ( 5 ) DEFAULT '00000' ;
DECLARE V_ORDLNSTATUSHISTID INT ;
DECLARE V_ORDERID INT DEFAULT 0 ;
DECLARE V_ORDERLINEID INT DEFAULT 0 ;
DECLARE V_NEWORDLINESTATUSCODE VARCHAR ( 10 ) DEFAULT '' ;
DECLARE V_OLDSTATUS VARCHAR ( 100 ) DEFAULT '' ;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
SET O_RESULT = SQLSTATE ;
SET O_RESULT = V_RESULT ;
SET V_ORDERID = ORDERID ;
SET V_ORDERLINEID = ORDERLINEID ;
SET V_NEWORDLINESTATUSCODE = NEWORDLINESTATUSCODE ;
/* Insert a new record for the new line OrdLineStatusHist*/
SET V_OLDSTATUS = ( SELECT OLSH . ORDLNSTATUSTYPECD FROM JOES . ORDERLINESTATUSHIST OLSH WHERE OLSH . CLIENTORDERID = V_ORDERID AND OLSH . CLIENTORDERLINEID = V_ORDERLINEID AND OLSH . STATUSENDDATETIME IS NULL ) ;
IF ( ( V_OLDSTATUS IS NULL ) OR ( V_OLDSTATUS <> V_NEWORDLINESTATUSCODE ) ) THEN
IF ( EXISTS ( SELECT ORDLNSTATUSHISTID FROM JOES . ORDERLINESTATUSHIST WHERE CLIENTORDERID = V_ORDERID AND CLIENTORDERLINEID = V_ORDERLINEID ) )
THEN
SET V_ORDLNSTATUSHISTID = ( SELECT MAX ( ORDLNSTATUSHISTID ) FROM JOES . ORDERLINESTATUSHIST WHERE CLIENTORDERID = V_ORDERID AND CLIENTORDERLINEID = V_ORDERLINEID ) ;
ELSE
SET V_ORDLNSTATUSHISTID = 0 ;
END IF ;
IF ( V_ORDLNSTATUSHISTID > 0 ) THEN
UPDATE JOES . ORDERLINESTATUSHIST SET STATUSENDDATETIME = CURRENT TIMESTAMP , LASTUPDATENAME = NAME , UPDTIMESTAMP = CURRENT TIMESTAMP WHERE
CLIENTORDERID = V_ORDERID AND CLIENTORDERLINEID = V_ORDERLINEID AND ORDLNSTATUSHISTID = V_ORDLNSTATUSHISTID ;
END IF ;
SET V_ORDLNSTATUSHISTID = V_ORDLNSTATUSHISTID + 1 ;
INSERT INTO JOES . ORDERLINESTATUSHIST ( CLIENTORDERID , CLIENTORDERLINEID , ORDLNSTATUSHISTID , ORDLNSTATUSTYPECD , STATUSSTARTDATETIME , STATUSENDDATETIME ,
CREATENAME , CREATETIMESTAMP ) VALUES ( V_ORDERID , V_ORDERLINEID , V_ORDLNSTATUSHISTID , V_NEWORDLINESTATUSCODE , CURRENT TIMESTAMP , CURRENT TIMESTAMP , NAME , CURRENT TIMESTAMP ) ;
UPDATE JOES . CLIENTORDERLINE SET
ORDLNSTATUSTYPECD = V_NEWORDLINESTATUSCODE , UPDTIMESTAMP = CURRENT TIMESTAMP WHERE CLIENTORDERID = V_ORDERID AND CLIENTORDERLINEID = V_ORDERLINEID ;
END IF ;
END ;
SET PATH "QSYS","QSYS2","JOES" ;
CREATE PROCEDURE JOES.CANCELTRANSFERNUMBER (
IN TRANSFERNUMBER INTEGER ,
OUT O_RETURNCODE CHAR(10) ,
OUT O_RETURNMESSAGE CHAR(50) ,
OUT O_RESULT CHAR(5) )
LANGUAGE SQL
SPECIFIC JOES.CANCELTRANSFERNUMBER
NOT DETERMINISTIC
MODIFIES SQL DATA
CALLED ON NULL INPUT
SET OPTION ALWBLK = *ALLREAD ,
ALWCPYDTA = *OPTIMIZE ,
COMMIT = *NONE ,
DECRESULT = (31, 31, 00) ,
DFTRDBCOL = *NONE ,
DYNDFTCOL = *NO ,
DYNUSRPRF = *USER ,
SRTSEQ = *HEX
BEGIN
DECLARE V_SYNONRTNCD CHAR ( 7 ) DEFAULT '' ;
DECLARE V_RETURMESSAGE CHAR ( 50 ) DEFAULT 'SUCCESS' ;
DECLARE V_RETURNCODE CHAR ( 10 ) DEFAULT '' ;
DECLARE V_TRANSFERNUMBER INTEGER ;
DECLARE V_CHARTRANSFERBATCHNUMBER CHAR ( 15 ) ;
DECLARE V_TRANSFERBATCHNUMBER INTEGER ;
DECLARE V_RESULT CHAR ( 5 ) DEFAULT '00000' ;
DECLARE SQLSTATE CHAR ( 5 ) DEFAULT '00000' ;
SET O_RESULT = SQLSTATE ;
SET O_RESULT = V_RESULT ;
SET V_TRANSFERNUMBER = TRANSFERNUMBER ;
SET V_TRANSFERBATCHNUMBER = ( SELECT MAX ( TRANSFERBATCH ) FROM JOES . TRANSFERREQHEADER WHERE TRANSFERNUMBER = V_TRANSFERNUMBER ) ;
SET V_CHARTRANSFERBATCHNUMBER = CHAR ( V_TRANSFERBATCHNUMBER ) ;
CALL MWWPROD . BAERXFR ( V_SYNONRTNCD , V_CHARTRANSFERBATCHNUMBER , V_RETURNCODE , V_RETURMESSAGE ) ;
DELETE FROM JOES . TRANSFERREQHEADER WHERE TRANSFERBATCH = V_TRANSFERBATCHNUMBER ;
DELETE FROM JOES . TRANSFERREQLINE WHERE TRANSFERBATCH = V_TRANSFERBATCHNUMBER ;
SET O_RETURNCODE = V_RETURNCODE ;
SET O_RETURNMESSAGE = V_RETURMESSAGE ;
END ;
위 파일을 별도의 파일로 분할하는 데 도움이 되는 스크립트를 작성하려고 합니다. 각 파일은 다음으로 끝나야 합니다.
경로 설정...
그리고 끝나는
끝 ;
그래서 출력은 다음과 같아야합니다
파일 1의 내용:
SET PATH "QSYS","QSYS2","JOES" ;
CREATE PROCEDURE JOES.CANCELORDERLINE (
IN ORDERID INTEGER ,
IN ORDERLINEID INTEGER ,
IN NEWORDLINESTATUSCODE VARCHAR(10) ,
IN NAME VARCHAR(50) ,
OUT O_RESULT CHAR(5) )
LANGUAGE SQL
SPECIFIC JOES.CANCELORDERLINE
NOT DETERMINISTIC
MODIFIES SQL DATA
CALLED ON NULL INPUT
SET OPTION ALWBLK = *ALLREAD ,
ALWCPYDTA = *OPTIMIZE ,
COMMIT = *NONE ,
DECRESULT = (31, 31, 00) ,
DFTRDBCOL = *NONE ,
DYNDFTCOL = *NO ,
DYNUSRPRF = *USER ,
SRTSEQ = *HEX
BEGIN ATOMIC
DECLARE V_ORDLINENUMFORDSP VARCHAR ( 16 ) ;
DECLARE V_ORDLINENUMSTR VARCHAR ( 3 ) ;
DECLARE V_ORDLINENUM INT ;
DECLARE V_RESULT CHAR ( 5 ) DEFAULT '00000' ;
DECLARE SQLSTATE CHAR ( 5 ) DEFAULT '00000' ;
DECLARE V_ORDLNSTATUSHISTID INT ;
DECLARE V_ORDERID INT DEFAULT 0 ;
DECLARE V_ORDERLINEID INT DEFAULT 0 ;
DECLARE V_NEWORDLINESTATUSCODE VARCHAR ( 10 ) DEFAULT '' ;
DECLARE V_OLDSTATUS VARCHAR ( 100 ) DEFAULT '' ;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
SET O_RESULT = SQLSTATE ;
SET O_RESULT = V_RESULT ;
SET V_ORDERID = ORDERID ;
SET V_ORDERLINEID = ORDERLINEID ;
SET V_NEWORDLINESTATUSCODE = NEWORDLINESTATUSCODE ;
/* Insert a new record for the new line OrdLineStatusHist*/
SET V_OLDSTATUS = ( SELECT OLSH . ORDLNSTATUSTYPECD FROM JOES . ORDERLINESTATUSHIST OLSH WHERE OLSH . CLIENTORDERID = V_ORDERID AND OLSH . CLIENTORDERLINEID = V_ORDERLINEID AND OLSH . STATUSENDDATETIME IS NULL ) ;
IF ( ( V_OLDSTATUS IS NULL ) OR ( V_OLDSTATUS <> V_NEWORDLINESTATUSCODE ) ) THEN
IF ( EXISTS ( SELECT ORDLNSTATUSHISTID FROM JOES . ORDERLINESTATUSHIST WHERE CLIENTORDERID = V_ORDERID AND CLIENTORDERLINEID = V_ORDERLINEID ) )
THEN
SET V_ORDLNSTATUSHISTID = ( SELECT MAX ( ORDLNSTATUSHISTID ) FROM JOES . ORDERLINESTATUSHIST WHERE CLIENTORDERID = V_ORDERID AND CLIENTORDERLINEID = V_ORDERLINEID ) ;
ELSE
SET V_ORDLNSTATUSHISTID = 0 ;
END IF ;
IF ( V_ORDLNSTATUSHISTID > 0 ) THEN
UPDATE JOES . ORDERLINESTATUSHIST SET STATUSENDDATETIME = CURRENT TIMESTAMP , LASTUPDATENAME = NAME , UPDTIMESTAMP = CURRENT TIMESTAMP WHERE
CLIENTORDERID = V_ORDERID AND CLIENTORDERLINEID = V_ORDERLINEID AND ORDLNSTATUSHISTID = V_ORDLNSTATUSHISTID ;
END IF ;
SET V_ORDLNSTATUSHISTID = V_ORDLNSTATUSHISTID + 1 ;
INSERT INTO JOES . ORDERLINESTATUSHIST ( CLIENTORDERID , CLIENTORDERLINEID , ORDLNSTATUSHISTID , ORDLNSTATUSTYPECD , STATUSSTARTDATETIME , STATUSENDDATETIME ,
CREATENAME , CREATETIMESTAMP ) VALUES ( V_ORDERID , V_ORDERLINEID , V_ORDLNSTATUSHISTID , V_NEWORDLINESTATUSCODE , CURRENT TIMESTAMP , CURRENT TIMESTAMP , NAME , CURRENT TIMESTAMP ) ;
UPDATE JOES . CLIENTORDERLINE SET
ORDLNSTATUSTYPECD = V_NEWORDLINESTATUSCODE , UPDTIMESTAMP = CURRENT TIMESTAMP WHERE CLIENTORDERID = V_ORDERID AND CLIENTORDERLINEID = V_ORDERLINEID ;
END IF ;
END ;
파일 2의 내용:
SET PATH "QSYS","QSYS2","JOES" ;
CREATE PROCEDURE JOES.CANCELTRANSFERNUMBER (
IN TRANSFERNUMBER INTEGER ,
OUT O_RETURNCODE CHAR(10) ,
OUT O_RETURNMESSAGE CHAR(50) ,
OUT O_RESULT CHAR(5) )
LANGUAGE SQL
SPECIFIC JOES.CANCELTRANSFERNUMBER
NOT DETERMINISTIC
MODIFIES SQL DATA
CALLED ON NULL INPUT
SET OPTION ALWBLK = *ALLREAD ,
ALWCPYDTA = *OPTIMIZE ,
COMMIT = *NONE ,
DECRESULT = (31, 31, 00) ,
DFTRDBCOL = *NONE ,
DYNDFTCOL = *NO ,
DYNUSRPRF = *USER ,
SRTSEQ = *HEX
BEGIN
DECLARE V_SYNONRTNCD CHAR ( 7 ) DEFAULT '' ;
DECLARE V_RETURMESSAGE CHAR ( 50 ) DEFAULT 'SUCCESS' ;
DECLARE V_RETURNCODE CHAR ( 10 ) DEFAULT '' ;
DECLARE V_TRANSFERNUMBER INTEGER ;
DECLARE V_CHARTRANSFERBATCHNUMBER CHAR ( 15 ) ;
DECLARE V_TRANSFERBATCHNUMBER INTEGER ;
DECLARE V_RESULT CHAR ( 5 ) DEFAULT '00000' ;
DECLARE SQLSTATE CHAR ( 5 ) DEFAULT '00000' ;
SET O_RESULT = SQLSTATE ;
SET O_RESULT = V_RESULT ;
SET V_TRANSFERNUMBER = TRANSFERNUMBER ;
SET V_TRANSFERBATCHNUMBER = ( SELECT MAX ( TRANSFERBATCH ) FROM JOES . TRANSFERREQHEADER WHERE TRANSFERNUMBER = V_TRANSFERNUMBER ) ;
SET V_CHARTRANSFERBATCHNUMBER = CHAR ( V_TRANSFERBATCHNUMBER ) ;
CALL MWWPROD . BAERXFR ( V_SYNONRTNCD , V_CHARTRANSFERBATCHNUMBER , V_RETURNCODE , V_RETURMESSAGE ) ;
DELETE FROM JOES . TRANSFERREQHEADER WHERE TRANSFERBATCH = V_TRANSFERBATCHNUMBER ;
DELETE FROM JOES . TRANSFERREQLINE WHERE TRANSFERBATCH = V_TRANSFERBATCHNUMBER ;
SET O_RETURNCODE = V_RETURNCODE ;
SET O_RETURNMESSAGE = V_RETURMESSAGE ;
END ;
이를 달성하기 위해 어떤 조치를 취할 수 있습니까? 나는 여기서 충격을 받았습니다.
답변1
내가 제공한 솔루션은 "SET PATH"로 각 파일을 시작합니다. 섹션 끝의 "END"를 확인하지 않습니다(그러나 선행하는 "END" 없이 후속 "SET PATH"가 발생할 경우 어떤 일이 발생하는지 설명하지 않았기 때문에 이것이 까다로운 요구 사항).
csplit -kz source.sql '/^SET PATH /' '{*}'
이 csplit
명령은 source.sql
줄 시작 부분에 고정된 "SET PATH" 문자열에 해당하는 정규식을 사용하여 파일을 분할합니다. 출력 파일의 이름을 제어하는 다양한 플래그가 있지만 기본적으로 예제의 출력은 파일 xx00
과 xx01
.