如何对xml文件中的特定值进行排序

如何对xml文件中的特定值进行排序,xml,sed,awk,Xml,Sed,Awk,我有一个xml文件,如下所示: <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 Percen

我有一个xml文件,如下所示:

<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>
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)
可能吗?我的工作真的需要这些:(请帮帮我,ppl。我会非常感谢你的帮助

#!/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)/'
这将显示

Last update: 17/09/2010 18:00:00
: 63.862 (% 0.20)
: 80.989 (% 0.25)
: 1.4970 (% 0.07)
: 1.9530 (% -0.31)
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)
我不知道IMKB、USD和EUR是什么意思,如果它们是基于百分比,那么你可以使用这个

#!/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/'
上面将显示

Last update: 17/09/2010 18:00:00
: 63.862 (% 0.20)
: 80.989 (% 0.25)
: 1.4970 (% 0.07)
: 1.9530 (% -0.31)
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)
我对sed不太了解,所以可能只有sed才有简单而简短的解决方案

更新 这是较短的版本

#!/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/'

是获取XML文件的地方,但它没有换行符,因此在使用curl时无法使用您的解决方案:(

我的目的是设计一个shell脚本,从上面提到的地址收集数据,然后显示有关股票交易所指数、黄金、美元和欧元的价格和价格变化的信息

我需要使用curl、awk、sed和grep来实现这一点,但我不知道如何实现,因为它需要复杂地使用shell编程技能。

希望这能有所帮助。 就连我都是Perl的新手。我只是试试运气

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;
打开配置,“example.dat”;
while()
{
如果($。==1)
{
s/*\“(\d+\/\d+\/\d+\d+:\d+:\d+\”*$/上次更新:$1/g;
}
如果(美元=2)
{
s/*\“(?\d+\.?\d+).*\”(\d+\.?\d+)。*$/A:$2(%$1)/g;
}
如果(美元=3)
{
s/*\“(?\d+\.?\d+).*\”(\d+\.?\d+)。*$/B:$2(%$1)/g;
}
如果(美元=4)
{
s/*\“(?\d+\.?\d+).*\”(\d+\.?\d+)。*$/C:$2(%$1)/g;
}
如果(美元=5)
{
s/*\“(?\d+\.?\d+).*\”(\d+\.?\d+)。*$/d:$2(%$1)/g;
}
s///i;
印刷品;
}
关闭配置;

好吧,这些百分位数不是静态的,所以我不能使用你的第二个解决方案。有4个变量称为IMKB、SD、USD、EUR,我希望它们显示在行的开头。好吧,谢谢你。这真的比没有好得多,我的朋友。没有办法添加输入中没有的信息,所以货币s将是一个挑战。您如何导出适当的货币缩写以显示?这些变量(IMKB、SD、USD、EUR)来自何处?