tags:

views:

71

answers:

3

Hi, all! I have an xml file as follows:

<Exchange DateTime="17/09/2010 18:00:00">
<Content Percent="0.20" Price="63.862" Sign="1"/>
<Content Percent="0.25" Price="80.989" Sign="1"/>
<Content Percent="0.07" Price="1.4970" Sign="1"/>
<Content Percent="-0.31" Price="1.9530" Sign="-1"/>
</Exchange>

There are 4 variables called A,B,C,D.

I need a script which outputs as follows:

Last update: 17/09/2010 18:00:00 
A: 63.862 (% 0.20) 
B: 80.989 (% 0.25)
C: 1.4970 (% 0.07) 
D: 1.9530 (% -0.31)

Is that possible? Well I really need that for my job :( Please help me, ppl. I will really appreciate your help!!

A: 
#!/bin/bash
echo Last update: `grep -o 'DateTime="[^"]*"' $1 | sed -e 's/DateTime="\([^"]*\)"/\1/'`
grep -o 'Percent="[^"]*" Price="[^"]*"' $1 | 
       sed -e 's/Percent="\([^"]*\)" Price="\([^"]*\)"/: \2 (% \1)/'

This will display

Last update: 17/09/2010 18:00:00
: 63.862 (% 0.20)
: 80.989 (% 0.25)
: 1.4970 (% 0.07)
: 1.9530 (% -0.31)

Didn't know what IMKB, USD and EUR means, if they are based on Percent then you can use this

#!/bin/bash
echo Last update: `grep -o 'DateTime="[^"]*"' $1 | sed -e 's/DateTime="\([^"]*\)"/\1/'`
grep -o 'Percent="[^"]*" Price="[^"]*"' $1 | 
       sed -e 's/Percent="\([^"]*\)" Price="\([^"]*\)"/: \2 (% \1)/' |
       sed -e 's/\(.*(% 0.20)\)/IMKB: \1/' | 
       sed -e 's/\(.*(% 0.07)\)/USD: \1/' |
       sed -e 's/\(.*(% -0.31)\)/EUR: \1/'

Above will display

Last update: 17/09/2010 18:00:00
IMKB: : 63.862 (% 0.20)
: 80.989 (% 0.25)
USD: : 1.4970 (% 0.07)
EUR: : 1.9530 (% -0.31)

I didn't know sed very well so mayby there is simple and shorter solusion using only sed.

Update This is shorter version

#!/bin/bash

echo Last update: `grep -o 'DateTime="[^"]*"' $1 | sed -e 's/DateTime="\([^"]*\)"/\1/'`
grep -o 'Percent="[^"]*" Price="[^"]*"' $1 | sed -e 's/Percent="\([^"]*\)" Price="\([^"]*\)"/: \2 (% \1)/' -e 's/\(.*(% 0.20)\)/IMKB\1/' -e 's/\(.*(% 0.07)\)/USD\1/' -e 's/\(.*(% -0.31)\)/EUR\1/' -e 's/\(.*(% 0.25)\)/SD\1/'

 

Last update: 17/09/2010 18:00:00
IMKB: 63.862 (% 0.20)
SD: 80.989 (% 0.25)
USD: 1.4970 (% 0.07)
EUR: 1.9530 (% -0.31)
jcubic
Well, those percentiles are not static so I can not use your 2nd solution. There are 4 variables called IMKB, SD, USD, EUR and I want them to be displayed in the beginning of the lines. Well, thank you, though. It is really much better than nothing, my friend.
Mehmet Guler
any other insights?
Mehmet Guler
A: 

http://cnnturk.com/finans/ticker/endeks.asp is where I get the XML file but it does NOT have newlines so I am not able to use your solution while using curl :(

My purpose is to come up with a shell script which gathers data from the address mentioned above and then displays information about both prices and price changes of stock exchange index, gold, usd, and euro.

I need to use curl, awk, sed, and grep to accomplish this but I can't figure out how because it requires complex use of shell programming skills.

Mehmet Guler
A: 

hope this helps. Even i am a newbie in Perl.just tried my luck.

open CONFIG,"example.dat";
    while(<CONFIG>)
    {
    if($.==1)
    {
     s/.*\"(\d+\/\d+\/\d+ \d+:\d+:\d+)\".*$/Last update: $1/g;
    }
    if($.==2)
    {
    s/.*\"(-?\d+\.?\d+).*\"(\d+\.?\d+)\".*$/A: $2 (% $1)/g;
    }
    if($.==3)
    {
    s/.*\"(-?\d+\.?\d+).*\"(\d+\.?\d+)\".*$/B: $2 (% $1)/g;
    }
    if($.==4)
    {
    s/.*\"(-?\d+\.?\d+).*\"(\d+\.?\d+)\".*$/C: $2 (% $1)/g;
    }
    if($.==5)
    {
    s/.*\"(-?\d+\.?\d+).*\"(\d+\.?\d+)\".*$/D: $2 (% $1)/g;
    }
    s/<\/exchange>//i;
    print ;
    }


close CONFIG;
Vijay Sarathi