Hi, all.
I need a shell script which gathers 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: cnnturk dot com/finans/ticker/endeks.asp
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'm not familiar with the syntax of churl, but assume that you can fetch the XML into a file to parse with awk. This is an example of one way to accomplish your task.
#!/usr/bin/env ksh
awk -v RS=">" ' # we dont need closing tag mark; make it the record seperator
/Percent/ { # data we are interested in has this field
data = 0;
gsub( " = ", "=" ); # easier to work with if there are no spaces round equal signs
gsub( "\"", "" ); # for this case we dont expect blanks in the value so ditch quote marks
for( i = 1; i < NF; i ++ ) # for each token that is var=value
{
if( (x = split( $(i), a, "=" )) == 2 ) # split and save the value
{
data++; # may not be necessary, but safe
var[a[1]] = a[2];
}
}
if( data ) # if we saw data, print it
{
printf( "%c %6.3f %6.3f%%\n", 65+idx, var["Price"], var["Percent"]);
idx++;
}
}' <current.xml
Change the printf() statement to print it in the format you need.
this is perfect.. thank you very much, agama. you really helped me out... this is what I was looking.. well, one last thing, how can i store the value of DateTime attribute in a variable??
thanks "again"!!!
Snarfing the date/time makes it a bit more complicated as the value has a space inside of the quotes. So, rewriting things a bit to allow for multiple tokens within quotes consider this brute force parsing example. You can use the date/time value as you need to. This just prints it out when it finds it.
#!/usr/bin/env ksh
awk -v RS=">" '
function snarf_vars( ) # capture all name=value pairs on the current line.
{
delete var;
gsub( " = ", "=" ); # easier to deal without spaces
for( i = 1; i <= NF; i ++ )
{
if( (x = split( $(i), a, "=" )) == 2 )
{
$(i) = a[2];
for( ; i <= NF; i++ ) # snarf up all tokens in the quotes
{
var[a[1]] = var[a[1]] $(i) " "; # capture next token
if( match( $(i), /"$/ ) ) # this has ending quote
{
gsub( "\"", "", var[a[1]] ); # one replace of all quotes (does not handle escaped quotes)
break;
}
}
}
}
}
/DateTime/{
snarf_vars();
printf( "%s \n", var["DateTime"] );
}
/Percent/ {
snarf_vars();
if( var["Price"] )
printf( "%c %6.3f %6.3f%%\n", 65+(idx++), var["Price"], var["Percent"]);
}' <test.xml
The function unsets the var array, so if you want the date/time value later you need to specifically save it or it will be gone. This also makes no attempt to preserve the whitespace that it might find between quotes; for your needs that'll be OK, but if you want to use this in other situations it might not do the job.