내 파일의 내용은 다음과 같습니다
#2211000000031#####{1:F01BKXXXX0AXXX0000000000}{2:I103BOTKJPJTXXXXN}{3:{121:faffba68-3ebe-4653-93fe-8b082ff226a5}}
{4:@@:20:EDCAK0010245@@:23B:CRED@@:32A:220303JPY10000,@@:33B:JPY10000,@@:50K:ABC@@WLG@@:52A:BKNZNZ20XXX@@:59:SUPER SERVICES LTD@@PO BOX 9999@@XX@@NEW YORK@@:70:/RFB/AUTOTEST-020356@@:71A:SHA@@-}
#2211000002311#####< Saa:Body>< AppHdr xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.02">< Fr>< FIId>< FinInstnId>< BICFI>BKNZNZ22985</ BICFI></ FinInstnId></ FIId></ Fr>< To>< FIId>< FinInstnId>< BICFI>BKNZNZ22985</ BICFI></ FinInstnId></ FIId></ To>< BizMsgIdr>2_1 Generic pacs 008</ BizMsgIdr>< MsgDefIdr>pacs.008.001.08</ MsgDefIdr>< BizSvc>swift.cbprplus.02</ BizSvc>< CreDt>2022-03-01T21:40:01+13:00</ CreDt></ AppHdr>< Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08">< FIToFICstmrCdtTrf>< GrpHdr>< MsgId>Generic Pacs 008</ MsgId>< CreDtTm>2021-12-09T07:08:54+12:00</ CreDtTm>< NbOfTxs>1</ NbOfTxs>< SttlmInf>< SttlmMtd>INDA</ SttlmMtd></ SttlmInf></ GrpHdr>< CdtTrfTxInf>< PmtId>< InstrId>Generic Pacs 008</ InstrId>< EndToEndId>Generic Pacs 008</ EndToEndId>< UETR>a19e9375-3e20-41ed-b75c-bb40d5afe540</ UETR></ PmtId>< IntrBkSttlmAmt Ccy="NZD">65.00</ IntrBkSttlmAmt>< IntrBkSttlmDt>2022-04-20</ IntrBkSttlmDt>< InstdAmt Ccy="NZD">1.00</ InstdAmt>< ChrgBr>SHAR</ ChrgBr>< PrvsInstgAgt1>< FinInstnId>< BICFI>NATAUS33</ BICFI></ FinInstnId></ PrvsInstgAgt1>< InstgAgt>< FinInstnId>< BICFI>BKNZNZ22985</ BICFI></ FinInstnId></ InstgAgt>< InstdAgt>< FinInstnId>< BICFI>BKNZNZ22985</ BICFI></ FinInstnId></ InstdAgt>< Dbtr>< Nm>REMITTING PERSON </ Nm>< PstlAdr>< StrtNm>A STREET NAME</ StrtNm>< BldgNb>999</ BldgNb>< BldgNm>THE BIG BUILDING</ BldgNm>< Flr>1</ Flr>< PstCd>1234</ PstCd>< TwnNm>A TOWN</ TwnNm>< TwnLctnNm>A COUNTY</ TwnLctnNm>< DstrctNm>WESTERN DISTRICT</ DstrctNm>< CtrySubDvsn>A STATE IN THE USA</ CtrySubDvsn>< Ctry>US</ Ctry></ PstlAdr></ Dbtr>< DbtrAgt>< FinInstnId/></ DbtrAgt>< CdtrAgt>< FinInstnId/ ></ CdtrAgt>< Cdtr>< Nm>A BENEFIARY PERSON</ Nm>< PstlAdr>< StrtNm>A BENEFICIARY ADDRESS</ StrtNm>< BldgNb>77</ BldgNb>< BldgNm>THE BUILDING WITH NO NAME</ BldgNm>< Flr>50</ Flr>< Room>4566</ Room>< PstCd>4556</ PstCd>< TwnNm>A BENEFICIARY TOWN</ TwnNm>< TwnLctnNm>A BENEFICIARY SUBURB</ TwnLctnNm>< DstrctNm>A DISTRICT</ DstrctNm>< CtrySubDvsn>A PROVINCE </ CtrySubDvsn>< Ctry>Cnty</ Ctry></ PstlAdr></ Cdtr>< CdtrAcct>< Id>< Othr>< Id>0209750998907040</ Id></ Othr></ Id></ CdtrAcct>< RmtInf>< Ustrd>REMITTANCE INFORMATION</ Ustrd></ RmtInf></ CdtTrfTxInf></ FIToFICstmrCdtTrf></ Document></ Saa:Body></ Saa:DataPDU>
#2223700000031#####<AppHdr xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.02"><Fr><FIId><FinInstnId><BICFI>BKNZ22985</BICFI></FinInstnId></FIId></Fr><To><FIId><FinInstnId><BICFI>ASBBNZ2AXXX</BICFI></FinInstnId></FIId></To><BizMsgIdr>AVP0000676232</BizMsgIdr><MsgDefIdr>pacs.004.001.10</MsgDefIdr><BizSvc>pnz.hvcs.01</BizSvc><CreDt>2022-08-25T09:36:45+12:00</CreDt></AppHdr><Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.004.001.10"><PmtRtr><GrpHdr><MsgId>BNZAVP0000676232</MsgId><CreDtTm>2022-08-25T09:36:45+12:00</CreDtTm><NbOfTxs>1</NbOfTxs><SttlmInf><SttlmMtd>CLRG</SttlmMtd><ClrSys><Cd>AVP</Cd></ClrSys></SttlmInf></GrpHdr><TxInf><RtrId>BNZAVP0000676232</RtrId><OrgnlGrpInf><OrgnlMsgId>ESAS.03808250935</OrgnlMsgId><OrgnlMsgNmId>pacs.008.001.09</OrgnlMsgNmId><OrgnlCreDtTm>2022-08-25T09:35:43+12:00</OrgnlCreDtTm></OrgnlGrpInf><OrgnlInstrId>ESAS.03808250935</OrgnlInstrId><OrgnlEndToEndId>E2ET.136</OrgnlEndToEndId><OrgnlUETR>875ddac6-d7f2-430c-86c0-a0f63cfdd387</OrgnlUETR><OrgnlIntrBkSttlmAmt Ccy="NZD">38.00</OrgnlIntrBkSttlmAmt><OrgnlIntrBkSttlmDt>2022-08-25</OrgnlIntrBkSttlmDt><RtrdIntrBkSttlmAmt Ccy="NZD">38</RtrdIntrBkSttlmAmt><IntrBkSttlmDt>2022-08-25</IntrBkSttlmDt><RtrdInstdAmt Ccy="XX">38.00</RtrdInstdAmt><ChrgBr>DEBT</ChrgBr><InstgAgt><FinInstnId><BICFI>BKNZNZ22985</BICFI><ClrSysMmbId><MmbId>BKNZNZ22985</MmbId></ClrSysMmbId></FinInstnId></InstgAgt><InstdAgt><FinInstnId><BICFI>2AXXX</BICFI></FinInstnId></InstdAgt><RtrChain><Dbtr><Pty><Nm>Test Customer</Nm><PstlAdr><TwnNm>XXXXXXX</TwnNm><Ctry>XX</Ctry></PstlAdr></Pty></Dbtr><DbtrAcct><Id><Othr><Id>0205730000000000</Id><SchmeNm><Cd>BBAN</Cd></SchmeNm></Othr></Id><Nm>Test Customer</Nm></DbtrAcct><Cdtr><Pty><Nm>Johnny Bravo</Nm><PstlAdr><AdrLine>12 Jellicoe Street</AdrLine><AdrLine>XXXXXX</AdrLine><AdrLine>NZ</AdrLine></PstlAdr></Pty></Cdtr><CdtrAcct><Id><Othr><Id>123166075056900</Id><SchmeNm><Cd>BBAN</Cd></SchmeNm></Othr></Id><Nm>PNZ Default</Nm></CdtrAcct></RtrChain><RtrRsnInf><Rsn><Cd>AC01</Cd></Rsn></RtrRsnInf></TxInf></PmtRtr></Document>
배경: 3개의 입력 라인이 있고, 3개 라인은 모두 3개의 개별 출력 파일로 이동됩니다... 3개 입력 라인의 차이점: 첫 번째 라인: 중괄호({})가 있습니다. 두 번째 라인에는 (< Saa: Body>< AppHdr)이 있습니다. 시작 줄 3의 xml 태그에는 시작의 xml 태그로서 (< AppHdr)만 있습니다.
출력 파일에는 다음 데이터가 포함됩니다.
첫 번째 파일:
#2211000000031#####{1:F01BKXXXX0AXXX0000000000}{2:I103BOTKJPJTXXXXN}{3:{121:faffba68-3ebe-4653-93fe-8b082ff226a5}}{4:@@:20:EDCAK0010245@@:23B:CRED@@:32A:220303JPY10000,@@:33B:JPY10000,@@:50K:ABC@@WLG@@:52A:BKNZNZ20XXX@@:59:SUPER SERVICES LTD@@PO BOX 9999@@XX@@NEW YORK@@:70:/RFB/AUTOTEST-020356@@:71A:SHA@@-}
두 번째 파일:
#2211000002311#####< Saa:Body>< AppHdr xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.02">< Fr>< FIId>< FinInstnId>< BICFI>BKNZNZ22985</ BICFI></ FinInstnId></ FIId></ Fr>< To>< FIId>< FinInstnId>< BICFI>BKNZNZ22985</ BICFI></ FinInstnId></ FIId></ To>< BizMsgIdr>2_1 Generic pacs 008</ BizMsgIdr>< MsgDefIdr>pacs.008.001.08</ MsgDefIdr>< BizSvc>swift.cbprplus.02</ BizSvc>< CreDt>2022-03-01T21:40:01+13:00</ CreDt></ AppHdr>< Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08">< FIToFICstmrCdtTrf>< GrpHdr>< MsgId>Generic Pacs 008</ MsgId>< CreDtTm>2021-12-09T07:08:54+12:00</ CreDtTm>< NbOfTxs>1</ NbOfTxs>< SttlmInf>< SttlmMtd>INDA</ SttlmMtd></ SttlmInf></ GrpHdr>< CdtTrfTxInf>< PmtId>< InstrId>Generic Pacs 008</ InstrId>< EndToEndId>Generic Pacs 008</ EndToEndId>< UETR>a19e9375-3e20-41ed-b75c-bb40d5afe540</ UETR></ PmtId>< IntrBkSttlmAmt Ccy="NZD">65.00</ IntrBkSttlmAmt>< IntrBkSttlmDt>2022-04-20</ IntrBkSttlmDt>< InstdAmt Ccy="NZD">1.00</ InstdAmt>< ChrgBr>SHAR</ ChrgBr>< PrvsInstgAgt1>< FinInstnId>< BICFI>NATAUS33</ BICFI></ FinInstnId></ PrvsInstgAgt1>< InstgAgt>< FinInstnId>< BICFI>BKNZNZ22985</ BICFI></ FinInstnId></ InstgAgt>< InstdAgt>< FinInstnId>< BICFI>BKNZNZ22985</ BICFI></ FinInstnId></ InstdAgt>< Dbtr>< Nm>REMITTING PERSON </ Nm>< PstlAdr>< StrtNm>A STREET NAME</ StrtNm>< BldgNb>999</ BldgNb>< BldgNm>THE BIG BUILDING</ BldgNm>< Flr>1</ Flr>< PstCd>1234</ PstCd>< TwnNm>A TOWN</ TwnNm>< TwnLctnNm>A COUNTY</ TwnLctnNm>< DstrctNm>WESTERN DISTRICT</ DstrctNm>< CtrySubDvsn>A STATE IN THE USA</ CtrySubDvsn>< Ctry>US</ Ctry></ PstlAdr></ Dbtr>< DbtrAgt>< FinInstnId/></ DbtrAgt>< CdtrAgt>< FinInstnId/ ></ CdtrAgt>< Cdtr>< Nm>A BENEFIARY PERSON</ Nm>< PstlAdr>< StrtNm>A BENEFICIARY ADDRESS</ StrtNm>< BldgNb>77</ BldgNb>< BldgNm>THE BUILDING WITH NO NAME</ BldgNm>< Flr>50</ Flr>< Room>4566</ Room>< PstCd>4556</ PstCd>< TwnNm>A BENEFICIARY TOWN</ TwnNm>< TwnLctnNm>A BENEFICIARY SUBURB</ TwnLctnNm>< DstrctNm>A DISTRICT</ DstrctNm>< CtrySubDvsn>A PROVINCE </ CtrySubDvsn>< Ctry>Cnty</ Ctry></ PstlAdr></ Cdtr>< CdtrAcct>< Id>< Othr>< Id>0209750998907040</ Id></ Othr></ Id></ CdtrAcct>< RmtInf>< Ustrd>REMITTANCE INFORMATION</ Ustrd></ RmtInf></ CdtTrfTxInf></ FIToFICstmrCdtTrf></ Document></ Saa:Body></ Saa:DataPDU>
세 번째 파일:
#2223700000031#####< AppHdr xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.02">< Fr>< FIId>< FinInstnId>< BICFI>BKNZ22985</ BICFI></ FinInstnId></ FIId></ Fr>< To>< FIId>< FinInstnId>< BICFI>ASBBNZ2AXXX</ BICFI></ FinInstnId></ FIId></ To>< BizMsgIdr>AVP0000676232</ BizMsgIdr>< MsgDefIdr>pacs.004.001.10</ MsgDefIdr>< BizSvc>pnz.hvcs.01</ BizSvc>< CreDt>2022-08-25T09:36:45+12:00</ CreDt></ AppHdr>< Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.004.001.10">< PmtRtr>< GrpHdr>< MsgId>BNZAVP0000676232</ MsgId></ Document>
UNIX 쉘 스크립트에 대한 나의 지식은 매우 제한적입니다. 행 수는 다양합니다.
답변1
awk를 사용하십시오.
$ awk '{print > ((/{/ ? "x" : "y") ".txt")}' file
$ head *.txt
==> x.txt <==
#2211000000031#####{1:F01BKXXXX0AXXX0000000000}{2:I103BOTKJPJTXXXXN}{3:{121:faffba68-3ebe-4653-93fe-8b082ff226a5}}
{4:@@:20:EDCAK0010245@@:23B:CRED@@:32A:220303JPY10000,@@:33B:JPY10000,@@:50K:ABC@@WLG@@:52A:BKNZNZ20XXX@@:59:SUPER SERVICES LTD@@PO BOX 9999@@XX@@NEW YORK@@:70:/RFB/AUTOTEST-020356@@:71A:SHA@@-}
#2211000000038#####{1:F01XXXX20AXXX0000000000}{2:I103BOTKJPJTXXXXN}{3:{121:50c659ec-6fb2-44a7-8312-26a270330aed}}{4:@@:20:ELCAK0020721@@:23B:CRED@@:32A:220303JPY1000,@@:33B:JPY1000,@@:50K:TESTAPP@@:52A:BKNZNZ20XXX@@:59:XYZ SERVICES LTD@@PO BOX 16130@@MARS@@CAL@@:70:/RFB/AUTOTEST-021013@@:71A:SHA@@-}
==> y.txt <==
#2211000002311#####< Saa:Body>< AppHdr xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.02">< Fr>< FIId>< FinInstnId>< BICFI>BKNZNZ22985</ BICFI></ FinInstnId></ FIId></ Fr>< To>< FIId>< FinInstnId>< BICFI>BKNZNZ22985</ BICFI></ FinInstnId></ FIId></ To>< BizMsgIdr>2_1 Generic pacs 008</ BizMsgIdr>< MsgDefIdr>pacs.008.001.08</ MsgDefIdr>< BizSvc>swift.cbprplus.02</ BizSvc>< CreDt>2022-03-01T21:40:01+13:00</ CreDt></ AppHdr>< Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08">< FIToFICstmrCdtTrf>< GrpHdr>< MsgId>Generic Pacs 008</ MsgId>< CreDtTm>2021-12-09T07:08:54+12:00</ CreDtTm>< NbOfTxs>1</ NbOfTxs>< SttlmInf>< SttlmMtd>INDA</ SttlmMtd></ SttlmInf></ GrpHdr>< CdtTrfTxInf>< PmtId>< InstrId>Generic Pacs 008</ InstrId>< EndToEndId>Generic Pacs 008</ EndToEndId>< UETR>a19e9375-3e20-41ed-b75c-bb40d5afe540</ UETR></ PmtId>< IntrBkSttlmAmt Ccy="NZD">65.00</ IntrBkSttlmAmt>< IntrBkSttlmDt>2022-04-20</ IntrBkSttlmDt>< InstdAmt Ccy="NZD">1.00</ InstdAmt>< ChrgBr>SHAR</ ChrgBr>< PrvsInstgAgt1>< FinInstnId>< BICFI>NATAUS33</ BICFI></ FinInstnId></ PrvsInstgAgt1>< InstgAgt>< FinInstnId>< BICFI>BKNZNZ22985</ BICFI></ FinInstnId></ InstgAgt>< InstdAgt>< FinInstnId>< BICFI>BKNZNZ22985</ BICFI></ FinInstnId></ InstdAgt>< Dbtr>< Nm>REMITTING PERSON </ Nm>< PstlAdr>< StrtNm>A STREET NAME</ StrtNm>< BldgNb>999</ BldgNb>< BldgNm>THE BIG BUILDING</ BldgNm>< Flr>1</ Flr>< PstCd>1234</ PstCd>< TwnNm>A TOWN</ TwnNm>< TwnLctnNm>A COUNTY</ TwnLctnNm>< DstrctNm>WESTERN DISTRICT</ DstrctNm>< CtrySubDvsn>A STATE IN THE USA</ CtrySubDvsn>< Ctry>US</ Ctry></ PstlAdr></ Dbtr>< DbtrAgt>< FinInstnId/></ DbtrAgt>< CdtrAgt>< FinInstnId/ ></ CdtrAgt>< Cdtr>< Nm>A BENEFIARY PERSON</ Nm>< PstlAdr>< StrtNm>A BENEFICIARY ADDRESS</ StrtNm>< BldgNb>77</ BldgNb>< BldgNm>THE BUILDING WITH NO NAME</ BldgNm>< Flr>50</ Flr>< Room>4566</ Room>< PstCd>4556</ PstCd>< TwnNm>A BENEFICIARY TOWN</ TwnNm>< TwnLctnNm>A BENEFICIARY SUBURB</ TwnLctnNm>< DstrctNm>A DISTRICT</ DstrctNm>< CtrySubDvsn>A PROVINCE </ CtrySubDvsn>< Ctry>Cnty</ Ctry></ PstlAdr></ Cdtr>< CdtrAcct>< Id>< Othr>< Id>0209750998907040</ Id></ Othr></ Id></ CdtrAcct>< RmtInf>< Ustrd>REMITTANCE INFORMATION</ Ustrd></ RmtInf></ CdtTrfTxInf></ FIToFICstmrCdtTrf></ Document></ Saa:Body></ Saa:DataPDU>
실제 입력에 {
s가 청크로 포함될 수 <
있고 그 반대의 경우도 질문에 제공한 예제를 편집하여 해당 사례와 기타 햇볕이 잘 들지 않는 사례를 포함하도록 테스트한 다음 정규식을 조정하여 조정할 수 있습니다.
답변2
이는 파일을 두 번 실행하므로 비효율적인 방법이지만 일회성인 경우 작업이 완료됩니다.
grep -e '#[0-9]\{13\}#####{.\+}' inputfilename >file1
grep -e '#[0-9]\{13\}#####<.\+>' inputfilename >file2
awk
입력 파일의 한 패스에서 두 파일을 모두 생성하므로 grep
위의 각 파일이 오랜 시간이 걸리는 경우 유용하므로 누군가가 더 나은 솔루션을 제공할 것이라는 데는 의심의 여지가 없습니다.