0

Content of my file is as following

#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>

Background: I have 3 input rows and all 3 rows will go into 3 separate output files... Difference between the 3 input lines: 1st row: has curly braces({}) 2nd row has (< Saa:Body>< AppHdr) as xml tags in the start 3rd row has only (< AppHdr) as xml tag in the start

Output files will have data like this :

First File:

#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@@-}  

Second File:

#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>

Third File:

#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>

I have very limited knowledge of UNIX shell scripts. The number of rows will vary.

7
  • 3
    Please edit your question and explain the logic we need to use. How should we split? How can we know what lines to put into file1 and what lines into file2? Also, what operating system are you using? Is this Linux? Unix? MacOS? Something else?
    – terdon
    Commented Aug 31, 2022 at 11:24
  • In the first 2 rows, every row has a ref id start and ends with #. Then the text message comes in curly braces. So first 2 rows have curly at start and at ends {}. While the 3rd row also starts with a ref id, but after that it has a xml tag starts with <saa: body> and ends with </ Saa:Body> So file 1 will have records with curly braces and file2 will have records with xml tags. the OS is Unix
    – user539635
    Commented Aug 31, 2022 at 11:42
  • 2
    Please EDIT your question and explain this. And please answer all of the questions. If you are really using UNIX, please tell us which one.
    – terdon
    Commented Aug 31, 2022 at 11:45
  • updated the questions with relevant information requested. Thank you.
    – user539635
    Commented Aug 31, 2022 at 11:55
  • 3
    @user539635 then please fix your question to make that clear Commented Aug 31, 2022 at 13:12

2 Answers 2

2

Using any 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>

If your real input can have {s in the < blocks or vice versa then edit the example you provided in your question for us to test with to include such cases plus any other non-sunny day cases and then we can tweak the regexp to suit.

7
  • Hi Ed, Just had another question, in your code, how can I make one file as .txt and another .xml. For example x.txt and y.xml. Kindly advise.
    – user539635
    Commented Sep 1, 2022 at 23:29
  • I really don't think the code's that hard to read/understand and even if you can't understand it for some reason there's really only a couple of possibilities so please try changing it in the way you think is obvious and then let me know if you can't figure it out.
    – Ed Morton
    Commented Sep 2, 2022 at 0:03
  • yes i have understand your code. both the output file(x and y) which your code is creating is .txt file. But i was just wondering is it possible to make one file with extension as .txt and another as .xml in the same code
    – user539635
    Commented Sep 2, 2022 at 0:32
  • I know what you're asking. The print > output is going to (/{/ ? "x" : "y") ".txt" which is a ternary expression (/{/ ? "x" : "y") with ".txt" concatenated on the end. So if you don't always want ".txt" concatenated to the end but instead want ".txt" or ".xml" then just include those in the ternary - (/{/ ? "x.txt" : "y.xml")
    – Ed Morton
    Commented Sep 2, 2022 at 0:39
  • 1
    thanks a lot.....output looks perfect as per my need. good learnings from your explanation.
    – user539635
    Commented Sep 2, 2022 at 0:53
0

Here's an inefficient way of doing it, since it runs through the file twice, but this will get the job done, if this is a one-off.

grep -e '#[0-9]\{13\}#####{.\+}' inputfilename >file1
grep -e '#[0-9]\{13\}#####<.\+>' inputfilename >file2

No doubt someone will provide an awk solution, which would be better since it could create both files in one pass-through the input file, useful if each grep above takes a long time.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .