-3

I have a very big TXT file which have records like this :

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

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

Now in the first row the unique character is curly braces ({}). So all records which have curly braces need to go into a separate file

The second row has xml tag starts with < Saa:Body>. So all records which have XML tag starts with < Saa:Body> needs to go into the second file.

And remaining records needs to go into the third file.

Kindly advise the UNIX shell script for the same.

5
  • I suggest to have a look at sed and the w command, using an address (=filter) pattern (like sed -e '/{/{w file1' -e 'd;};/< Saa:Body>/{w file 2 and so on. Should be quite simple, but just ask if you encounter any problem.
    – Philippos
    Sep 13, 2022 at 11:57
  • I was trying to use awk like this way, but able to separate only 2 input rows awk '{print > (/{/ ? "x.txt" : "y.xml")}' file1.txt Wanna need your help in creating the third file
    – user539635
    Sep 13, 2022 at 12:02
  • Please consider explaining the difference of this question to your previous one; it might attract close votes otherwise.
    – AdminBee
    Sep 13, 2022 at 12:02
  • in my previous question i have posted to create 2 output file based on only 1 input identifier. Now I have 2 input identifiers and need the 3 output file for all the 3 input rows.
    – user539635
    Sep 13, 2022 at 12:06
  • 2
    And did you try to adapt the solution from your previous question to this one? If so, please EDIT your question and show us what you tried and how it failed. This will both help us avoid wasting your time or ours with solutions you have already tried, and it will show that you have put some effort into it. This site is not designed as a free script-writing service, and people tend to get annoyed if they feel that users come here and just expect us to do their work for them with no effort on their part. So showing us what you have tried is really important.
    – terdon
    Sep 13, 2022 at 13:57

1 Answer 1

1

I can't understand what is so hard about completing the attempt I gave in the comment, but I'll try with better explanation:

sed -n '/{/{w file1
    d
  }
  /< Saa:Body>/{w file2
    d
  }
  w file3' yourfile

Lines 1–3 and 4–6 follow the same pattern:

  • an address pattern /pattern/
  • a pair of braces to enclose the commands to execute if the pattern matches, namely:
    • a write command to write the current line to a given file
    • a delete command to stop further processing of that line

Put together that means for lines 1–3: If the line contains a {, write that line to file1 and stop further processing of that line, so everything else only applies for lines without {.

The lines 4–6 do the same thing for pattern < Saa:Body>, but write to file2 and you could add as many other patterns and files as you want (don't dare to ask a question how to do it with a fourth file!).

Finally, line 7 doesn't need a pattern: It writes everything else(lines that didn't match either pattern) to file3.

The -n option suppresses default output, because you probably don't want to see it.

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