3

如何从所有元素中提取“失败”并将它们相加?

<gateway>
    <smscs>
        <count>3</count>
        <smsc>
            <id>a</id>
            <received><sms>0</sms><dlr>0</dlr></received>
            <sent><sms>10537</sms><dlr>0</dlr></sent>
            <failed>13</failed>
            <queued>6272</queued>
        </smsc>
        <smsc>
            <id>b</id>
            <received><sms>0</sms><dlr>0</dlr></received>
            <sent><sms>10530</sms><dlr>0</dlr></sent>
            <failed>10</failed>
            <queued>6284</queued>
        </smsc>
        <smsc>
            <id>c</id>
            <received><sms>0</sms><dlr>0</dlr></received>
            <sent><sms>10679</sms><dlr>0</dlr></sent>
            <failed>11</failed>
            <queued>6291</queued>
        </smsc>
    </smscs>
</gateway>
4

2 回答 2

4

我只是用

xmlstarlet sel -t -v "sum(/gateway/smscs/smsc/failed)" -n input.xml

返回

34

这个想法是使用一个sum()函数,它接受一个节点集并返回所有元素的字符串值转换为数字的总和。

于 2013-07-19T11:45:00.250 回答
0

解决了

xmlstarlet sel -t -m "gateway/smscs/smsc/failed" -v "." -n | awk '{s+=$1} END {print s}'
于 2013-07-19T11:53:38.240 回答