Sed get xml attribute value

12,841

You don't even need a pattern /trade_id_external/ before the s///

$ sed -n 's/.*trade_id_external="\([^"]*\).*/\1/p' file
001-002141880445/5862

In basic sed , \(...\) called capturing groups which was used to capture the characters you want to print at the final.

Through grep,

$ grep -oP 'trade_id_external="\K[^"]*' file
001-002141880445/5862

-P would turn on the Perl-regex mode in grep. So we could use any PCRE regexes in grep with -P param enabled. \K in the above regex would discard the previously matched characters, that is, it won't consider the characters which are matched by the pattern exists before the \K

Share:
12,841
Iurii
Author by

Iurii

Updated on July 23, 2022

Comments

  • Iurii
    Iurii almost 2 years

    I have next xml file:

    <AutoTest>
     <Source>EBS FX</Source>
     <CreateCFF>No</CreateCFF>
     <FoXML descriptor="pb.fx.spotfwd.trade.feed" version="2.0">
       <FxSpotFwdTradeFeed>
         <FxSpotFwd feed_datetime="17-Dec-2014 10:20:09" 
           cpty_sds_id="EBS" match_id="L845586141217" original_trade_id_feed="L80107141217" 
           value_date="20141218" trade_id_external="001-002141880445/5862" match_sds_id="EBSFeedCpty" 
           counter_ccy="USD" trade_id_feed="107" trade_type="S" feed_source_id="80"    quoting_term="M" 
           deal_ccy="GBP" rate="1.5" trade_date="20141217" modified_by="automation"    cpty_side="B" counter_amt="1500000"
           smart_match="0" booking_status_id="10" trade_status_id="22" deal_amt="1000000"  trade_direction="B">
           <Notes />
         </FxSpotFwd>
     </FxSpotFwdTradeFeed>
     <TestCases />
     </FoXML>
    </AutoTest>
    

    How to get value of trade_id_external attribute by using sed?
    I tried with this expression: sed -n '/trade_id_external/s/.*=//p' ./file.xml but no luck