Extracting vaules from end of string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting vaules from end of string
# 1  
Old 11-30-2016
Extracting vaules from end of string

I'm trying to write a script which will calculate percentages from a printer.
I have a command
Code:
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):
Code:
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?



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-30-2016 at 08:36 AM.. Reason: Added CODE tags.
# 2  
Old 11-30-2016
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.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-30-2016
Quote:
Originally Posted by RudiC
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.
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
Code:
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
Code:
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'.

Last edited by rbatte1; 11-30-2016 at 10:06 AM.. Reason: Added ICODE tags for output
# 4  
Old 11-30-2016
Assuming a recent bash: try using an array:
Code:
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
Code:
echo ${ARR[-1]}
1800

This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-30-2016
Quote:
Originally Posted by leshy93
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
Code:
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
Code:
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'.
How about:
Code:
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
Code:
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
This User Gave Thanks to apmcd47 For This Post:
# 6  
Old 11-30-2016
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 ----------

Quote:
Originally Posted by RudiC
Assuming a recent bash: try using an array:
Code:
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
Code:
echo ${ARR[-1]}
1800

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

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

Quote:
Originally Posted by apmcd47
How about:
Code:
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
Code:
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
syntax error: operand expected (error token is "*100/ ") on line
Code:
cyanperc=$((  ${currentcyan##* } * 100 / ${maxcyan##* } ))

# 7  
Old 11-30-2016
If you carefully scrutinize my proposal and your statement leading to the error msg, you might find the mistake...
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting substring within string between 2 token within the string

Hello. First best wishes for everybody. here is the input file ("$INPUT1") contents : BASH_FUNC_message_begin_script%%=() { local -a L_ARRAY; BASH_FUNC_message_debug%%=() { local -a L_ARRAY; BASH_FUNC_message_end_script%%=() { local -a L_ARRAY; BASH_FUNC_message_error%%=() { local... (3 Replies)
Discussion started by: jcdole
3 Replies

2. UNIX for Dummies Questions & Answers

Extracting an ipaddress and using it to send files but error at the end of file

i want to extract ip address from a file and using that ip address copy file to systems. set fid set content close $fid ## Split into records on newlines set records send "records splited\n" send $records\n set timeout 600 set a "test\n" send $a foreach rec $records { ##... (1 Reply)
Discussion started by: amature_rach
1 Replies

3. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

4. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

5. Shell Programming and Scripting

Appending string, variable to file at the start and string at end

Hi , I have below file with 13 columns. I need 2-13 columns seperated by comma and I want to append each row with a string "INSERT INTO xxx" in the begining as 1st column and then a variable "$node" and then $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13 and at the end another string " ; COMMIT;" ... (4 Replies)
Discussion started by: Vaddadi
4 Replies

6. Shell Programming and Scripting

Extracting String

I am trying to extract a hyperlink from a html document using awk. I have managed to output in the format... href="index.html"> where i would like it just to output index.html. Any ideas on how i would do this? Thanks (2 Replies)
Discussion started by: adpe
2 Replies

7. UNIX for Dummies Questions & Answers

reading vaules from a text file.

say i have a text file called "input.txt" 1 5 2 10 3 15 4 20 and say I wanted to read the row 3 from column 2 how would i go about it. I guess I would use "awk" but I'm not sure how. (2 Replies)
Discussion started by: THM
2 Replies

8. Shell Programming and Scripting

extracting a string

Hi All, I am writing a shell script for which I am stuck with an extraction part. I arrived till extraction of a path of file. Lets take an example. I now have a file which contains following one line: 2348/home/userid/mydir/any_num_dir/myfile.text Now I want to extract only... (2 Replies)
Discussion started by: start_shell
2 Replies

9. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

10. Shell Programming and Scripting

Unique cell vaules in a file

I have a file in which I have records as follows -------------------------------------------------------------------------- 2-sister-birmbb-s0005.ftip002590790.vpn-nte.bt.net BRI0 0 0 0 aber-con-dyce-s0006.ftip002732992.vpn-nte.bt.net ATM0 0 0 0... (3 Replies)
Discussion started by: rahulrathod
3 Replies
Login or Register to Ask a Question