Grep xml tags

Hi

I want to get the value between to XML tags as follows

<EAN>12345</EAN>

so i would want to return 12345. i have tried sed and awk but can't do it.

can anyone help?

Try this.

sed -n -e 's/<EAN>\([0-9][0-9]*\)<\/EAN>/\1/p' xmlfile

Vino

That doesn't seem to work

My XML is as follows and i want to get the value between the UpcEan flags

<Baskets xsi:noNamespaceSchemaLocation="C:/Tom/SGWREA~1/ParserPlan/mapping2/workInProgress/RtiOutput.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Basket><BHead><Store>890</Store><TradingDate>2005-07-11</TradingDate><TxnDateTime>2005-06-26 15:58:25</TxnDateTime><TillBankId>2</TillBankId><PosNo>102</PosNo><PosTxnNo>237</PosTxnNo><CashierId>512</CashierId><TxnVoidInd>N</TxnVoidInd><NoSale>0</NoSale><StyOrderNumber>0</StyOrderNumber><BagCount>0</BagCount><BagValue>0</BagValue><CustomerCountInd>Y</CustomerCountInd><ReplayInd>N</ReplayInd></BHead><BItems><BItem><LongShortSaleInd>S</LongShortSaleInd><ClassGroup>#NotFound#</ClassGroup><UpcEan>0000001199888</UpcEan><CancelItemInd>N</CancelItemInd><SubtractItemInd>N</SubtractItemInd><UnknownItemInd>N</UnknownItemInd><CompItemInd>N</CompItemInd><LinkedItemInd>N</LinkedItemInd><GroupSaleInd>N</GroupSaleInd><DepartmentMode>0</DepartmentMode><ExtendPrice>199</ExtendPrice><Quantity>1</Quantity><UnitPrice>199</UnitPrice><AtForQty>0</AtForQty><PriceFromPlu>#NotFound#</PriceFromPlu><KeyedPrice>0</KeyedPrice><SysReductionId>0</SysReductionId><ReductionInd>N</ReductionInd><RefundItemInd>N</RefundItemInd><RefundReasonCode>0</RefundReasonCode><ItemEntryInd>S</ItemEntryInd><WeightVolume>0</WeightVolume><DriveOffInd>N</DriveOffInd><PennyPerLitreDiscount>0</PennyPerLitreDiscount><FuelItemInd>N</FuelItemInd><PumpTestInd>N</PumpTestInd><GradePrice>0</GradePrice></BItem><BItem><LongShortSaleInd>S</LongShortSaleInd><ClassGroup>#NotFound#</ClassGroup><UpcEan>0000001199888</UpcEan><CancelItemInd>N</CancelItemInd><SubtractItemInd>N</SubtractItemInd><UnknownItemInd>N</UnknownItemInd><CompItemInd>N</CompItemInd><LinkedItemInd>N</LinkedItemInd><GroupSaleInd>N</GroupSaleInd><DepartmentMode>0</DepartmentMode><ExtendPrice>199</ExtendPrice><Quantity>1</Quantity><UnitPrice>199</UnitPrice><AtForQty>0</AtForQty><PriceFromPlu>#NotFound#</PriceFromPlu><KeyedPrice>0</KeyedPrice><SysReductionId>0</SysReductionId><ReductionInd>N</ReductionInd><RefundItemInd>N</RefundItemInd><RefundReasonCode>0</RefundReasonCode><ItemEntryInd>S</ItemEntryInd><WeightVolume>0</WeightVolume><DriveOffInd>N</DriveOffInd><PennyPerLitreDiscount>0</PennyPerLitreDiscount><FuelItemInd>N</FuelItemInd><PumpTestInd>N</PumpTestInd><GradePrice>0</GradePrice></BItem></BItems><SubTotals><SubTotal><ItemCount>2</ItemCount><SubTotalValue>398</SubTotalValue></SubTotal></SubTotals><Tenders><Tender><THead><TenderTypeId>1</TenderTypeId><CancelItemInd>N</CancelItemInd><SubtractItemInd>N</SubtractItemInd><TenderValue>2000</TenderValue><MonetaryChange>0</MonetaryChange><ConvRate>0</ConvRate><OtherValue>0</OtherValue><TenderNo>1</TenderNo></THead></Tender></Tenders></Basket></Baskets>
sed -n '/<EAN>/,/<\/EAN>/ {
                            s/.*<EAN>\(.*\)<\/EAN>.*$/\1/p
                         }'

bakunin

That didn't work either.

did it work for you?

sed -n  -e 's/.*<UpcEan>\([0-9][0-9]*\)<\/UpcEan>.*/\1/p' handak9.xml

handak9.xml is the input xml file.

Vino

Thanks - but that doesn't work - have you tried it?

Yes, tried and successful.

[~/temp]$ sed -n  -e 's/.*<UpcEan>\([0-9][0-9]*\)<\/UpcEan>.*/\1/p' ean.xml
0000001199888

ean.xml is your xml file.

The only reason, I see is probably a different sed or a different OS altogether.

Post the following from your machine.

uname -a

Vino

Yes i am passing in a xml file.

I am using a Sun Solaris

[FIT01]:uname -a
SunOS stevux1095d 5.8 Generic_117350-11 sun4u sparc SUNW,Sun-Fire-15000

From a solaris machine, this is what I tried and got.

$ uname -a
SunOS XXXXXXX 5.8 Generic_108528-24 sun4u sparc SUNW,UltraAX-i2

$ sed -n -e 's/.*<UpcEan>\([0-9][0-9]*\)<\/UpcEan>.*/\1/p' temp.xml
0000001199888

Is there any error that is thrown on the screen when you run your sed? If yes, what is it ?

Vino