Extracting vaules from end of string

I'm trying to write a script which will calculate percentages from a printer.
I have a command

snmpwalk -v1 -c public 192.168.0.20 1.3.6.1.2.1.43.11.1.1.9.1.1

which returns (current ink level):

iso.3.6.1.2.1.43.11.1.1.9.1.1 = INTEGER 235

then a similar command which returns similar output but has 1800 at the end which is the max ink value.

I need a way of extracting the numbers from the end and inserting them into variables.

I did it with rev but the problem is that the amount of numbers at the end of the string may vary.

Can anyone help me with it?

Welcome to the forum.

It's usually good habit to also post the OS and shell versions of your system, the tools you prefer to achieve your goals, and, on top of the input samples, also some desired output.
In your above case, using shell arrays for the solution comes to mind, but proposing something without knowing the shell version might be in vain.

1 Like

Thank you for the information.
I'm working on a Raspberry Pi 3 running Ubuntu Mate. GNU, version 4.3.46(1)

From the bash script I run:
first command

currentcyan=$(snmpwalk -v1 - c public 192.168.0.20 1.3.6.1.2.1.43.11.1.1.9.1.1)

which returns iso.3.6.1.2.1.43.11.1.1.9.1.1 = INTEGER 235

second command

maxcyan=$(snmpwalk -v1 -c public 192.168.0.20 1.3.6.1.2.1.43.11.1.1.8.1.1)

which returns iso.3.6.1.2.1.43.11.1.1.8.1.1 = INTEGER 1800

I already assigned the output of the commands to variables now I need a flexible way of extracting the number after 'INTEGER'.

Assuming a recent bash : try using an array:

ARR=( $(snmpwalk -v1 -c public 192.168.0.20 1.3.6.1.2.1.43.11.1.1.8.1.1) )
echo ${ARR[${#ARR[@]}-1]}
1800

EDIT: or

echo ${ARR[-1]}
1800
1 Like

How about:

currentcyan=$(snmpwalk -v1 - c public 192.168.0.20 1.3.6.1.2.1.43.11.1.1.9.1.1)
currentcyan=${currentcyan##* }
maxcyan=$(snmpwalk -v1 -c public 192.168.0.20 1.3.6.1.2.1.43.11.1.1.8.1.1)
maxcyan=${maxcyan##* }

or possibly

currentcyan=$(snmpwalk -v1 - c public 192.168.0.20 1.3.6.1.2.1.43.11.1.1.9.1.1)
maxcyan=$(snmpwalk -v1 -c public 192.168.0.20 1.3.6.1.2.1.43.11.1.1.8.1.1)
cyanperc=$((  ${currentcyan##* } * 100 / ${maxcyan##* } ))

The ${var##*x removes everything (thanks to the *) from the beginning to the last x (which in the above example is a space) One # is a "non-greedy" version while using %% and % work from the end.

Andrew

1 Like

Guys, thank you for the responses but I've been an idiot. With my mind in chaos I mistyped the outcome of the command.

The correct format is iso.3..6.1.2.1.43.11.1.1.9.1.1 = INTEGER: 235

Same format for the second command.

I honestly apologise

---------- Post updated at 03:40 PM ---------- Previous update was at 03:20 PM ----------

I get "${ARR{-1}}: bad substitution" in the console

---------- Post updated at 03:42 PM ---------- Previous update was at 03:40 PM ----------

syntax error: operand expected (error token is "*100/ ") on line

cyanperc=$((  ${currentcyan##* } * 100 / ${maxcyan##* } ))

If you carefully scrutinize my proposal and your statement leading to the error msg, you might find the mistake...

1 Like

I finally understood how ##* works and corrected my code:

cyanperc=$(( ${currentcyan##* }*100/${maxcyan##* } ))

Worked perfectly fine :slight_smile:

Thank you for your help and for not telling me what was wrong so that I could figure it out myself. I really appreciate it!

Lucas