从xml属性中查找并执行数学表达式并替换该值

时间:2013-05-17 11:52:36

标签: xml shell math sed awk

以下是一些需要处理的xml文件的摘录:

<BirimAdi>Adet</BirimAdi>
<BirimCarpan>1</BirimCarpan>
<HavaleFiyati>0</HavaleFiyati>
<HavaleFiyatiParaBirimi>TL</HavaleFiyatiParaBirimi>
<Price1>0</Price1>
<SatisFiyati1ParaBirimi>TL</SatisFiyati1ParaBirimi>
<Isk1>0</Isk1>
<SatisFiyati2>0</SatisFiyati2>

我需要做的是获取标签之间的值,并对其进行以下数学运算。

Price1 = round(Price1)-0.1;

脚本应该对指定路径中的所有xml文件执行此操作。

我考虑使用'sed'或'awk',但我不确定这可以在sed中轻松完成。使用xmllint对我来说太过分了。有任何想法吗?我是这些实用程序的新手,所以无法想到找到我想要的那个正则表达式是:

/<\s*Price1[^>]*>([^<]*)<\s*\/\s*Price1\s*>/

2 个答案:

答案 0 :(得分:7)

我会使用XML解析器来完成这项工作。例如,XML::Twig。这是一个例子:

#!/usr/bin/env perl

use warnings;
use strict;
use XML::Twig;

for my $f ( @ARGV ) {
        my $twig = XML::Twig->new(
                twig_handlers => {
                        'Price1' => sub { $_->set_text( sprintf( "%.1f", int( $_->text) - 0.1 ) ) },
                },
                pretty_print => 'indented',
        )->parsefile( $f )->print;
}

假设文件名为script.pl,测试文件为xmlfile且内容为:

<root>
<BirimAdi>Adet</BirimAdi>
<BirimCarpan>1</BirimCarpan>
<HavaleFiyati>0</HavaleFiyati>
<HavaleFiyatiParaBirimi>TL</HavaleFiyatiParaBirimi>
<Price1>3.3</Price1>
<SatisFiyati1ParaBirimi>TL</SatisFiyati1ParaBirimi>
<Isk1>0</Isk1>
<SatisFiyati2>0</SatisFiyati2>
</root>

像以下一样运行:

perl script.pl xmlfile

产量:

<root>
  <BirimAdi>Adet</BirimAdi>
  <BirimCarpan>1</BirimCarpan>
  <HavaleFiyati>0</HavaleFiyati>
  <HavaleFiyatiParaBirimi>TL</HavaleFiyatiParaBirimi>
  <Price1>2.9</Price1>
  <SatisFiyati1ParaBirimi>TL</SatisFiyati1ParaBirimi>
  <Isk1>0</Isk1>
  <SatisFiyati2>0</SatisFiyati2>
</root>

答案 1 :(得分:1)

一个快速的解决方案:

perl -pe 's!<(Price1)>(\d+(?:\.\d*)?)</\1>!"<$1>".(int($2+0.5)-0.1)."</$1>"!e'<<XXX
<HavaleFiyatiParaBirimi>TL</HavaleFiyatiParaBirimi>
<Price1>2.3</Price1>
<SatisFiyati1ParaBirimi>TL</SatisFiyati1ParaBirimi>
<Price1>2.5</Price1>
XXX

输出:

<HavaleFiyatiParaBirimi>TL</HavaleFiyatiParaBirimi>
<Price1>1.9</Price1>
<SatisFiyati1ParaBirimi>TL</SatisFiyati1ParaBirimi>
<Price1>2.9</Price1>

但Birei的解决方案到目前为止更好......