I have a file like this. I need to copy a specific tag into another tag and for each section where the tag is present.
**<Idcode>123456</Idcode>
<DrctDbtTxInf>
<InstrId>XXXXXXXXXXXX</InstrId>
<EndToEndId>XXXXXXXXXXXXXXX</EndToEndId>
<InstdAmt Ccy="EUR">XXXXXXXXXXXXXXX</InstdAmt>
<MndtId>XXXXXXXXXXXXXXX</MndtId>
<DtOfSgntr>XXXXXXXXXXXXX</DtOfSgntr>
<AmdmntInd>XXXXXXXXXXXXXXX</AmdmntInd>
<Nm>XXXXXXXXXXXXX</Nm>
<IBAN>XXXXXXXXXXXXXXXXXXXX</IBAN>
<Ustrd>XXXXXXXXXXXXXXXXXX</Ustrd>
</DrctDbtTxInf>
<DrctDbtTxInf>
<InstrId>XXXXXXXXXXXXXXXXXX</InstrId>
<EndToEndId>XXXXXXXXXXXXXXXXXXXXXX</EndToEndId>
<InstdAmt Ccy="EUR">XXXXXXXXXXXXXXXXXXX</InstdAmt>
<MndtId>XXXXXXXXXXXXXXXXXXXXXX</MndtId>
<DtOfSgntr>XXXXXXXXXXXXXX</DtOfSgntr>
<AmdmntInd>XXXXXXXXXXXXXXXXXXXXX/AmdmntInd>
<Nm>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</Nm>
<IBAN>XXXXXXXXXXXXXXXXXXXXXXXXXXX</IBAN>
<Ustrd>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</Ustrd>
</DrctDbtTxInf>**
and I would have a result like this:
**<Idcode>123456</Idcode>
<DrctDbtTxInf>
<Idcode>123456</Idcode>
<InstrId>XXXXXXXXXXXX</InstrId>
<EndToEndId>XXXXXXXXXXXXXXX</EndToEndId>
<InstdAmt Ccy="EUR">XXXXXXXXXXXXXXX</InstdAmt>
<MndtId>XXXXXXXXXXXXXXX</MndtId>
<DtOfSgntr>XXXXXXXXXXXXX</DtOfSgntr>
<AmdmntInd>XXXXXXXXXXXXXXX</AmdmntInd>
<Nm>XXXXXXXXXXXXX</Nm>
<IBAN>XXXXXXXXXXXXXXXXXXXX</IBAN>
<Ustrd>XXXXXXXXXXXXXXXXXX</Ustrd>
</DrctDbtTxInf>
<DrctDbtTxInf>
<Idcode>123456</Idcode>
<InstrId>XXXXXXXXXXXXXXXXXX</InstrId>
<EndToEndId>XXXXXXXXXXXXXXXXXXXXXX</EndToEndId>
<InstdAmt Ccy="EUR">XXXXXXXXXXXXXXXXXXX</InstdAmt>
<MndtId>XXXXXXXXXXXXXXXXXXXXXX</MndtId>
<DtOfSgntr>XXXXXXXXXXXXXX</DtOfSgntr>
<AmdmntInd>XXXXXXXXXXXXXXXXXXXXX/AmdmntInd>
<Nm>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</Nm>
<IBAN>XXXXXXXXXXXXXXXXXXXXXXXXXXX</IBAN>
<Ustrd>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</Ustrd>
</DrctDbtTxInf>**
Considering the limit of my bash version: 4.4.23(1)-release.
Thanks for any help!
I tried to seach a topic like this but it's also complicated to search it
CodePudding user response:
If I understand the task:
grep does not help much, you find the Idcode and DrctDbtTxInf lines, but then you are pretty stuck
paste is not helpful at all (I think). Paste is used to merge entire files
I think you could be successful just using sed, but that would be perverse
I believe that a shell script using most any shell would work, and not be that difficult (if you are up on your shell scripting)
Here is a one-liner using gawk (awk) that is probably shorter than a shell equivalent:
gawk '{print}/**<Idcode>/{sub(/**/,"");I=$0}/<DrctDbtTxInf>/{print I}' < ourInputFile
- read next line & print it
- if the line matches **<Idcode>, chop off asterisks and name result I
- if the line matches <DrctDbtTxInf>, print I