sum numbers in different lines in shell
i have 8000 lines similar to this
<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">
and what i need to do is summing rxPackets in each line together and txPackets too
i mean total sum of them in 8000 lines
thanks in advance
<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">
Solution 1:
You can use this small awk script for 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
Solution 2:
Using XML::XSH2, a wrapper around XML::LibXML:
open file.xml ;
$sum = 0 ;
for //Flow $sum = $sum + @rxPackets + @txPackets ;
echo $sum ;