如何从所有元素中提取"失败"并将其相加?
<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>
我只是简单地使用了
xmlstarlet sel -t -v "sum(/gateway/smscs/smsc/failed)" -n input.xml
返回
34
其思想是使用sum()
函数,该函数获取一个节点集,并返回所有元素的字符串值转换为数字的总和。
使用解决
xmlstarlet sel -t -m "gateway/smscs/smsc/failed" -v "." -n | awk '{s+=$1} END {print s}'