Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting an XML element

Status
Not open for further replies.

ddrillich

Technical User
Jun 11, 2003
546
US
Good Day,

We have a daily cron job that captures an XML document for statistics purposes. This large XML file ends up being in two lines.

I would like to extract the value of first occurrence of the documents element that looks like -

Code:
<documents>1615453</documents>

Any ideas?

Regards,
Dan

 
Does grep -o '<documents>[0-9]+</documents>' filename.xml produce what you need, or does it match multiple items?

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Thank you Annihilannic!

Code:
[autonomy@sbkj2ksrchdahs01 statistics]$ grep -o '<documents>[0-9]*</documents>'  stats_docs_11-01-2011 | head -1
<documents>1617752</documents>

One more thing - how can I get the number itself?

Regards,
Dan
 
Not much luck with -

Code:
[autonomy@sbkj2ksrchdahs01 statistics]$ grep -o '<documents>[0-9]*</documents>'  stats_docs_11-01-2011 | head -1 | grep -o '[0-9]*'
[autonomy@sbkj2ksrchdahs01 statistics]$
 
Hi

If you want only the value, it is simple with XmlStarlet:
Code:
xml sel -t -v '//documents' stats_docs_11-01-2011
By the way, your code at 2 Nov 11 11:20 works for me.

Feherke.
 
Feherke, weird as it doesn't work for me. Right, I should have such an app that maybe supports XPath...

Regards,
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top