2016-06-14 96 views
0

我有一个XML响应,每当BillPlan =“UBM - A/A”时,我需要从中提取ObjDevice的值。能否请你帮我一个shell脚本做同样的Shell脚本从XML响应中提取变量的值

<Txn> 
      <TxnName>Spectrum-AlertSite-Component_spectrum.engprod_InstantUpgradeEdge_GetProductOffersByAccount_getProductOffersByAccountValid</TxnName> 
      <TxnDetail Monitor="y" Notify="y" PingError="n" 
    TraceError="y" Interval="5" TimeOut="300" 
    ObjDevice="318373" BillPlan="UBM - A/A" MaxSteps="50" 

/> 
     </Txn> 
     <Txn> 
      <TxnName>Spectrum-AlertSite-Component_spectrum.engprod_InstantUpgradeEdge_GetProductOffersByAccount_getProductOffersByAccountValid</TxnName> 
      <TxnDetail Monitor="y" Notify="y" PingError="n" 
    TraceError="y" Interval="5" TimeOut="300" 
    ObjDevice="318377" BillPlan="UBM - A/A" MaxSteps="50" 

/> 
+0

你看任何其他的“我怎么提取XML文档中的壳价值”的问题吗? –

回答

1

那么,如果你不这样做有一个特殊的工具来处理XML文件,并且你总是在同一行中有两个值,那么你可以执行:

grep 'BillPlan="UBM - A/A"' | sed 's,.*ObjDevice="\([0-9]*\)" BillPlan="UBM - A/A".*,\1," 

很抱歉,如果我不使用SED进行过滤,但不那么熟悉SED还没有获得一个* nix的,但也许别人可以给我们的短版

+0

在您的'sed'命令中,您应该以's'开头或将它与[sed -ne'/ BillPlan =“UBM -A \/A”/ s,。* ObjDevice =“\([0-9] * \)“BillPlan =”UBM - A/A“。*,\ 1,p'' –

1

使用XMLStarlet

xmlstarlet sel -t -m '//TxnDetail[@BillPlan="UBM - A/A"]' \ 
        -v ./@ObjDevice -n \ 
    <in.xml 
1

用我Xidel你可以这样做:

xidel /tmp/foo.xml -e '//TxnDetail[@BillPlan="UBM - A/A"]/@ObjDevice' 
1

我asume libxml2安装(很可能任何GNU/Linux系统上):

xmllint --xpath '//TxnDetail[@BillPlan="UBM - A/A"]/@ObjDevice' input.xml