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

发布于 2024-09-24 02:08:17 字数 576 浏览 2 评论 0原文

我有一个如下的 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>

有 4 个变量,称为 A、B、C、D。

我需要一个输出如下的脚本:

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)

这可能吗?嗯,我的工作真的需要这个:(请帮助我,朋友。我将非常感谢你的帮助!

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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黄昏下泛黄的笔记 2024-10-01 02:08:17
#!/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)

不知道 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
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/'

版本

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)
#!/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)
讽刺将军 2024-10-01 02:08:17

http://cnnturk.com/finans/ticker/endeks.asp 是我的位置获取 XML 文件,但它没有换行符,因此我无法在使用curl 时使用您的解决方案:(

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

我需要使用curl、awk、sed 和grep 来完成此任务,但我不知道如何实现,因为它需要复杂的shell 编程技能。

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.

单身狗的梦 2024-10-01 02:08:17

希望这有帮助。
即使我是 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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文