Sed get xml attribute value

I have the following 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 the value of the trade_id_external attribute using sed?
I tried with this expression: sed -n '/trade_id_external/s/.*=//p' ./file.xml but no luck

+6
source share
1 answer

You do not even need the template /trade_id_external/ before s///

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

Basic sed \(...\) refers to capture groups that were used to capture the characters you want to print in the final.

Via grep ,

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

-P will enable perl-regex mode in grep. Therefore, we could use any PCRE regular expression in grep with the -P option. \K in the above regular expression will cancel previously matched characters, that is, it will not consider characters that match the pattern, exists before \K

+13
source

Source: https://habr.com/ru/post/979919/


All Articles