LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-30-2013, 01:06 AM   #1
karish
LQ Newbie
 
Registered: Jan 2013
Posts: 8

Rep: Reputation: Disabled
How to assign value from a egrep command to variable on bash and manipulate it?


Hi,

I have following output (multiple occurrence with different value) from my router saved in file Test.data

ROUTERX#
ROUTERX-ROUTERY-FA-1/0/1
sh policy-map interface Fa1/0/1 output | utility egrep "Cla$
Tue Jan 29 21:50:34.510
Class A
Matched : 14454886/2669417187 20
Class B
Matched : 17199039574/3726691396348 49398
Class C
Matched : 231087111507/313843060325282 6021822
Class D
Matched : 26748876/24423409936 161
Class E
Matched : 2656823/1666119504 1
Class F
Matched : 4248541409/2263014784977 30401
Class class-default
Matched : 569200029009/697693816143396 22268590

How can I get the value for Class C, which is 6021822, and divide it by 1000 and display the result?

Following is what I tried.

#!/bin/bash

class1=`egrep -A 16 "FA-1/0/1" Test.data | grep -A 1 "C" | egrep -w "Matched" | sed -e "s/ \+ / /g" | cut -d " " -f 5`

class1_mbps=$(($class1/1000))

printf "%d\n" "$class1_mbps"

exit 0

Not getting any answer ...Any suggestions would be highly appreciated.

Thank you.
 
Old 01-30-2013, 02:12 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Firstly bash does not perform decimal calculations so dividing by a 1000 will not yield what you expect.

As for the solution, I would use awk and search for the line you wish, "Class C", then ask for the next line and print the last field.
 
Old 01-30-2013, 03:23 AM   #3
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
your
Code:
sed -e "s/ \+ / /g"
does nothing. Change it to
Code:
class1=`egrep -A 16 "FA-1/0/1" Test.data | grep -A 1 "C" | egrep -w "Matched" | cut -d " " -f 4`
class1_mbps=$(echo $class1'/1000'|bc)
and use bc to calculate you could also strip the last 3 characters.
 
Old 01-30-2013, 11:15 AM   #4
karish
LQ Newbie
 
Registered: Jan 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
Following is used to remove multiple white spaces with one.
Code:
sed -e "s/ \+ / /g"
Executing following in shell yields 6021822 and doesn't work on the code shown below.
egrep -A 16 "FA-1/0/1" Test.data | grep -A 1 "C" | egrep -w "Matched" | sed -e "s/ \+ / /g" | cut -d " " -f 5

Executing following in shell yields 123456 and works on the code shown below.
egrep -w "Matched" test1.data | cut -d " " -f 2`

Following is the code that shows what's working and what's not working.

Code:
#!/bin/bash

class1=`egrep -A 16 "FA-1/0/1" Test.data | grep -A 1 "C" | egrep -w "Matched" | sed -e "s/ \+ / /g" | cut -d " " -f 5`

class1_mbps=`echo "$class1/1000" | bc -l` 

printf "%0.3f\n" "$class1_mbps"   ########## This doesn't work ##############

test_class1=`egrep -w "Matched" test1.data | cut -d " " -f 2`

test_class1_mbps=`echo "$class1/1000" | bc -l` 

printf "%0.3f\n" "$test_class1_mbps"  ############ This works ############

exit 0
Please advise what's wrong with the "egrep" statement that is not working. Thank you.

Last edited by karish; 01-30-2013 at 11:24 AM.
 
Old 01-30-2013, 12:26 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Well as suggested:
Code:
awk '/Class C/{getline; print $NF/1000}' Test.data
 
2 members found this post helpful.
Old 01-30-2013, 04:01 PM   #6
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
Change
Code:
cut -d " " -f 5 to
cut -d " " -f 4
 
Old 01-30-2013, 05:32 PM   #7
karish
LQ Newbie
 
Registered: Jan 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
Solved

Thanks Grail. It worked.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH: Assign a variable to a variable name... c_henry Programming 9 06-04-2012 12:33 PM
Assign Command value to a variable rabir Linux - Newbie 10 05-21-2012 10:01 AM
bash script: how to assign command ouput to a variable without executing it? bostonantifan Programming 1 02-13-2011 12:55 AM
[SOLVED] [bash] assign boolean expression to variable hashbang#! Programming 10 08-16-2009 09:44 AM
bash script: how to assign an output (not the result) to a variable? Singing Banzo Programming 8 10-01-2006 07:29 PM


All times are GMT -5. The time now is 02:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration