Issue
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>XXX</InstrId>
<EndToEndId>XXX</EndToEndId>
<InstdAmt Ccy="EUR">XXX</InstdAmt>
<MndtId>XXX</MndtId>
<DtOfSgntr>XXX</DtOfSgntr>
<AmdmntInd>XXX</AmdmntInd>
<Nm>XXX</Nm>
<IBAN>XXX</IBAN>
<Ustrd>XXX</Ustrd>
</DrctDbtTxInf>
<DrctDbtTxInf>
<InstrId>XXX</InstrId>
<EndToEndId>XXX</EndToEndId>
<InstdAmt Ccy="EUR">XXX</InstdAmt>
<MndtId>XXX</MndtId>
<DtOfSgntr>XXX</DtOfSgntr>
<AmdmntInd>XXX</AmdmntInd>
<Nm>XXX</Nm>
<IBAN>XXX</IBAN>
<Ustrd>XXX</Ustrd>
</DrctDbtTxInf>
and I would have a result like this:
<Idcode>123456</Idcode>
<DrctDbtTxInf>
<Idcode>123456</Idcode>
<InstrId>XXX</InstrId>
<EndToEndId>XXX</EndToEndId>
<InstdAmt Ccy="EUR">XXX</InstdAmt>
<MndtId>XXX</MndtId>
<DtOfSgntr>XXX</DtOfSgntr>
<AmdmntInd>XXX</AmdmntInd>
<Nm>XXX</Nm>
<IBAN>XXX</IBAN>
<Ustrd>XXX</Ustrd>
</DrctDbtTxInf>
<DrctDbtTxInf>
<Idcode>123456</Idcode>
<InstrId>XXX</InstrId>
<EndToEndId>XXX</EndToEndId>
<InstdAmt Ccy="EUR">XXX</InstdAmt>
<MndtId>XXX</MndtId>
<DtOfSgntr>XXX</DtOfSgntr>
<AmdmntInd>XXX</AmdmntInd>
<Nm>XXX</Nm>
<IBAN>XXX</IBAN>
<Ustrd>XXX</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
Solution
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
Answered By - sroush Answer Checked By - Clifford M. (WPSolving Volunteer)