0

i have the following SNMP Command

snmpget -v 1 -c COMMUNITY -t 2 HOST:161  1.3.6.1.4.1.24681.1.2.1.0 | awk '{print $4 $4}' | sed 's/.\(.*\)...../\1/'

that returns a string

9.10

what is percent value in the worng decimal.

I want to multiplicate this with 10 to make it correct:

91.0

How can I do this with the command above?

Thanks! John


The snmpget command outputs the string

iso.3.6.1.4.1.24681.1.2.1.0 = STRING: "9.10 %"
AdminBee
  • 21,637
  • 21
  • 47
  • 71
johnripper
  • 11
  • 3
  • 1
    Does this answer your question? [How to do integer & float calculations, in bash or other languages/frameworks?](https://unix.stackexchange.com/questions/40786/how-to-do-integer-float-calculations-in-bash-or-other-languages-frameworks) – alecxs Jan 29 '21 at 12:02
  • `awk '{print $4 $4}'` seems strange... – JJoao Jan 29 '21 at 18:52
  • Welcome to the site. For future reference, please be sure to add the "raw" output of the command whose output you are text-processing, so that contributors can more easily track down the origin of unwanted output behavior. – AdminBee Feb 01 '21 at 07:33

1 Answers1

1

Use one awk command like this:

# Use two sepatators, <space> and ", so you catch exactly the number.
# Multiply by 10
# `printf` allows you to format numbers in many ways, in this case, 1 decimal

str='iso.3.6.1.4.1.24681.1.2.1.0 = STRING: "9.10 %"'

$ echo "$str" | awk -F'[ "]' '{ printf "%.1f\n", $5*10 }'
91.0

Or as pointed out by @Stéphane Chazelas, this works too:

echo "$str" | awk -F \" '{ printf "%.1f\n", $2*10 }'

Modifiers for printf Formats

%e, %E, %f, %F
Number of digits to the right of the decimal point.`

schrodingerscatcuriosity
  • 12,087
  • 3
  • 29
  • 57