我有下一个xml文件:
<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>
Run Code Online (Sandbox Code Playgroud)
如何使用sed 获取trade_id_external属性的值?
我尝试了这个表达方式:sed -n '/trade_id_external/s/.*=//p' ./file.xml
但没有运气
Avi*_*Raj 20
你甚至都不需要一个模式/trade_id_external/之前,s///
$ sed -n 's/.*trade_id_external="\([^"]*\).*/\1/p' file
001-002141880445/5862
Run Code Online (Sandbox Code Playgroud)
在基本sed中,\(...\)称为捕获组,用于捕获要在最终打印的字符.
通过grep,
$ grep -oP 'trade_id_external="\K[^"]*' file
001-002141880445/5862
Run Code Online (Sandbox Code Playgroud)
-P将打开grep中的Perl-regex模式.所以我们可以在grep中使用任何PCRE正则表达式并-P启用param.\K在上面的正则表达式中将丢弃先前匹配的字符,也就是说,它不会考虑在模式之前存在与模式匹配的字符\K