0

I have a XML response from which I need to extract the value of ObjDevice whenever the BillPlan="UBM - A/A". Can you please help me with a shell script to do the same

    <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"

 />
1
  • Have you looked at any of the other "how do I extract a value from an XML document in shell" questions? Commented Jun 14, 2016 at 20:24

4 Answers 4

1

Well if you don't have a special tool to handle XML files and you always have in the same line both values then you can execute:

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

Sorry if I don't use sed to filter but not so familiar with sed and also don't have access to a *nix, but maybe somebody else can give us the short version

Sign up to request clarification or add additional context in comments.

1 Comment

In your sed command you should start with a s or combine it to sed -ne '/BillPlan="UBM - A\/A"/s,.*ObjDevice="\([0-9]*\)" BillPlan="UBM - A/A".*,\1,p'
1

Using XMLStarlet:

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

Comments

1

With my Xidel you can do:

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

Comments

1

I asume libxml2 is installed (quite likely on any GNU/Linux system):

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.