How to add up fields

Hi everyone,

after entering the following command:

/usr/sfw/bin/snmpwalk -v2c -c "sMart5snmPaCC3ss" ABI0-QCLAMI-CE2 1.3.6.1.4.1.9.9.43.1.1.6.1.7 

I get this output:

SNMPv2-SMI::enterprises.9.9.43.1.1.6.1.7.24 = INTEGER: 194
SNMPv2-SMI::enterprises.9.9.43.1.1.6.1.7.25 = INTEGER: 194
SNMPv2-SMI::enterprises.9.9.43.1.1.6.1.7.26 = INTEGER: 194
SNMPv2-SMI::enterprises.9.9.43.1.1.6.1.7.27 = INTEGER: 194
SNMPv2-SMI::enterprises.9.9.43.1.1.6.1.7.28 = INTEGER: 194
SNMPv2-SMI::enterprises.9.9.43.1.1.6.1.7.29 = INTEGER: 194
SNMPv2-SMI::enterprises.9.9.43.1.1.6.1.7.30 = INTEGER: 194
SNMPv2-SMI::enterprises.9.9.43.1.1.6.1.7.31 = INTEGER: 194
SNMPv2-SMI::enterprises.9.9.43.1.1.6.1.7.32 = INTEGER: 194
SNMPv2-SMI::enterprises.9.9.43.1.1.6.1.7.33 = INTEGER: 194

As you see, all output lines end with 194 which is a number. I need a pipeline command (attached to the original command above) to get all these numbers, add them up and store them in a variable called TOTAL. so in this case TOTAL should contain 194*10 or 1940

Does anyone know how to do this??

Any sugestions will be highly appreciated:D
omoyne

Something like this?

command | awk '{t+=$NF}END{print t}' file
1 Like

the format is correct but it does not work. it won't store anything in the variable:(

Do you want to store the output in a shell variable?

var=`command | awk '{t+=$NF}END{print t}' file`

or:

var=$(command | awk '{t+=$NF}END{print t}' file)

If I am not wrong then probably file has been mentioned unintentionally.

Should be something like

var=$(command | awk '{t+=$NF}END{print t}')
2 Likes