在 shell 中对不同行中的数字求和

在 shell 中对不同行中的数字求和

我有 8000 行类似内容

 <Flow flowId="1" timeFirstTxPacket="+0.0ns" timeFirstRxPacket="+924100.0ns" timeLastTxPacket="+199984927558.0ns" timeLastRxPacket="+199675473275.0ns" delaySum="+2287566662167.0ns" jitterSum="+65280162191.0ns" lastDelay="+3511349483.0ns" txBytes="161956" rxBytes="116536" txPackets="3125" rxPackets="2251" lostPackets="640" timesForwarded="0">

我需要做的是将每行中的 rxPackets 和 txPackets 加在一起

我指的是 8000 行的总和

提前致谢

<Flow flowId="1" timeFirstTxPacket="+0.0ns" timeFirstRxPacket="+924100.0ns" timeLastTxPacket="+199984927558.0ns" timeLastRxPacket="+199675473275.0ns" delaySum="+2287566662167.0ns" jitterSum="+65280162191.0ns" lastDelay="+3511349483.0ns" txBytes="161956" rxBytes="116536" txPackets="3125" rxPackets="2251" lostPackets="640" timesForwarded="0">

<Flow flowId="2" timeFirstTxPacket="+3000000.0ns" timeFirstRxPacket="+3254190.0ns" timeLastTxPacket="+199988489445.0ns" timeLastRxPacket="+199631899307.0ns" delaySum="+12081144328484.0ns" jitterSum="+105606015634.0ns" lastDelay="+9407845452.0ns" txBytes="251896" rxBytes="124840" txPackets="4867" rxPackets="2413" lostPackets="1757" timesForwarded="0">

<Flow flowId="3" timeFirstTxPacket="+4000000.0ns" timeFirstRxPacket="+4254098.0ns" timeLastTxPacket="+199971756470.0ns" timeLastRxPacket="+198959480315.0ns" delaySum="+5150205600987.0ns" jitterSum="+74009967633.0ns" lastDelay="+9862217799.0ns" txBytes="163044" rxBytes="90532" txPackets="3150" rxPackets="1751" lostPackets="1013" timesForwarded="0">

答案1

你可以使用这个小 awk 脚本来GNU awk

awk 'BEGIN {x=0;y=0} { match($13,/([0-9]+)/,a); x = x + a[1]; match($12,/([0-9]+)/,b); y = y + b[1]} END {print "rxPackets:" x " txPackets:" y}' yourfile

答案2

使用XML::XSH2,包装器XML::LibXML

open file.xml ;
$sum = 0 ;
for //Flow $sum = $sum + @rxPackets + @txPackets ;
echo $sum ;

相关内容