1

I have a file which has ReferenceId="0128140321960O" and also RelatedReferenceId="SWCC"

<FinancialTransfersInstruction xmlns="middleware.frk.com/Schemas/Onetis/1.0">
<SWIFTHeader DeliveryMethod="SWIFT" MessageType="202" OrigSendDate="2019-11-15" Receiver="SBOSUS3UIMS" ReferenceId="0207060050180O" Sender="TESTBIC" SenderName="TEST INVESTORS, INC" TransType="NEW"/>
<PortalHeader BusinessUnit="GTS"/> <Wires> <Wire Currency="USD" Id="20706s5018.0" RelatedReferenceId="CCPM" </Wire> </Wires>
<FinancialTransfersInstruction/></FinancialTransfersInstruction>

When I run my command to extract the value, my result gives me related reference too; I only want the ReferenceId tag value.

cat *.xml |grep -P -o -e '(?<=ReferenceId=").*?(?=")'
  • grep -P -o -e '^ReferenceId=\K.*' file.xml (\K means Forget everything till now) – George Vasiliou Nov 16 '19 at 8:32
  • hi Thank you for the quick response, unfortunately it doesn't return a value. – Nightfly Nov 16 '19 at 18:23
  • Please add a full line example that contains the text of interest. – seshoumara Nov 17 '19 at 6:49
  • Hi i'm a doing this on redhat linux 7. to reproduce this please create a text file with the following line it, – Nightfly Nov 17 '19 at 13:17
  • 1
    The XML that you have posted in a comment should be added to the question. Also, that XML seems to be severely broken and can't be parsed by an XML parser. – Kusalananda Nov 19 '19 at 15:33
2

The XML in your question is invalid. Assuming your corrected XML is actually this,

<?xml version="1.0"?>
<FinancialTransfersInstruction xmlns="https://middleware.frk.com/Schemas/Onetis/1.0">
  <SWIFTHeader DeliveryMethod="SWIFT" MessageType="202" OrigSendDate="2019-11-15" Receiver="SBOSUS3UIMS" ReferenceId="0207060050180O" Sender="TESTBIC" SenderName="TEST INVESTORS, INC" TransType="NEW"/>
  <PortalHeader BusinessUnit="GTS"/>
  <Wires>
    <Wire Currency="USD" Id="20706s5018.0" RelatedReferenceId="CCPM"/>
  </Wires>
</FinancialTransfersInstruction>

you can use xmlstarlet to parse the XML and to extract the appropriate attribute value

xmlstarlet sel -t -v '//_:SWIFTHeader/@ReferenceId' bankfile.xml
0207060050180O
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.