Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-21-2010, 07:44 PM   #1
LQ Newbie
Registered: Sep 2010
Posts: 2

Rep: Reputation: 0
Cool Sorting specific values from an XML file

Hi, all.
I need a shell script which gathers the data from a remote XML file and then displays it according to my needs.. I need this for my job due to the fact that I need to keep track price changes of euro, usd, gold, etc.
The XML file I am talking about is located at this page: here.
The reason I am posting the URL is that I need to use curl to get this file and it does NOT have newlines after each tag. I thought that that would be a problem.
Here is what I need from the script:
1) curl to get the page
2) make use of sed, awk, etc. to display its contents in a more structured and readable manner as shown below:
A: 64.125 (% -0.26)
B: 81.130 (% -0.32)
C: 1.4930 (% 0.00)
D: 1.9590 (% 0.36)

I am looking forward to your insights..
Old 09-21-2010, 08:00 PM   #2
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
Is there a particular reason you have to use BASH rather than a scripting language like perl, python, or ruby? All of these languages have mechanisms for parsing XML and grabbing webpage contents that making writing such a utility much easier.

I'm not saying it's not possible in BASH just that it would require more than a simple little script.


Old 09-21-2010, 08:05 PM   #3
LQ Newbie
Registered: Sep 2010
Posts: 2

Original Poster
Rep: Reputation: 0
perl and python are also welcome. There is no specific reason that I prefer shell scripting over those languages.. I also think that it is a really tough job to accomplish this task under bash even though I do not know a little bit on programming

Last edited by canimsin; 09-21-2010 at 08:06 PM.
Old 09-21-2010, 08:29 PM   #4
Registered: Apr 2010
Posts: 228

Rep: Reputation: 46
#!/usr/bin/env ruby -w

require 'net/http'
require 'rexml/document'
include REXML
response = Net::HTTP.get_response(url)
xml.elements.each("Exchange/Content") do |element|
    print "Percent: #{element.attributes["Percent"]}, Price: #{element.attributes["Price"] }\n"

$ ruby test.rb
Percent: -0.26, Price: 64.125
Percent: -0.32, Price: 81.130
Percent: 0.00, Price: 1.4930
Percent: 0.36, Price: 1.9590
Old 09-21-2010, 08:35 PM   #5
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
You could use xsltproc to transform the xml file according to your .xslt transform file.

Also, using 'xmllint --format' could reformat the xml file making it easier to use sed or awk if you wish to.

Last edited by jschiwal; 09-21-2010 at 08:41 PM.
Old 09-21-2010, 10:48 PM   #6
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 335

Rep: Reputation: 141Reputation: 141
curl -s | 
grep -o 'Content[^>]*' |
sed 's/[^0-9".-]//g
s/"\(.*\)""\(.*\)"".*/: \2 (% \1)/

A: 64.125 (% -0.26)
B: 81.130 (% -0.32)
C: 1.4930 (% 0.00)
D: 1.9590 (% 0.36)
1 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
(PHP, XML) need help sorting through a multi-dimensional array gatorpower Programming 1 09-17-2010 12:34 PM
Modifying Specific Child Nodes In XML using Shell Script senthilmuthiah Linux - Newbie 1 04-20-2009 04:38 AM
how to grep for only the values of a specific field hchoonbeng Linux - Newbie 3 11-19-2008 08:20 AM
Perl: Sorting by occurrence of specific character Poetics Programming 5 09-26-2007 04:46 PM
Sorting all files of home folder modified after a specific date for backup fc6_user Linux - General 2 03-30-2007 02:24 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:04 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration