LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 09-02-2010, 06:04 PM   #1
santius
LQ Newbie
 
Registered: Sep 2010
Location: Montevideo, Uruguay
Distribution: Ubuntu 10.04 LTS
Posts: 5

Rep: Reputation: 0
Question Average of a column in console output


Hi guys,

I need to create a script in order to get the average jitter value of all asterisk call using "iax2 show netstat".

Now using grep I receive the result as:

Quote:
RTT Jit Del Lost % Drop OOO Kpkts Jit Del Lost % Drop OOO Kpkts
As you can see, I need to sum the Jit column and divide the result between the line count.

How is the best way for this? My return should be a Integer, for example: "2".

Thanks!
 
Old 09-02-2010, 07:48 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
var=$(iax2 show netstat)
set -- $var
echo $(($2 + $9))

Last edited by ghostdog74; 09-02-2010 at 07:51 PM.
 
Old 09-03-2010, 11:20 AM   #3
santius
LQ Newbie
 
Registered: Sep 2010
Location: Montevideo, Uruguay
Distribution: Ubuntu 10.04 LTS
Posts: 5

Original Poster
Rep: Reputation: 0
Question

Quote:
Originally Posted by ghostdog74 View Post
Code:
var=$(iax2 show netstat)
set -- $var
echo $(($2 + $9))
Wow! It's really works... thank you very much ghostdog74!

I have one doubt, what exactly does
Quote:
set -- $var
?

Does it assign the output to the variable? And if it does, then the meaning of:

Quote:
echo $(($2 + $9))
Is: Give me the value in second file column 9?

Thanks again! I googled for "set --" but did not found the answer...
 
Old 09-03-2010, 11:43 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
set -- [values] sets the $1..$2..$3...etc. positional parameters to those values. Note that this will overwrite any values that existed there before. See the builtin commands section of man bash for more.

$((..)) is the bash built in simple arithmetic function. Note that it only works with whole integers.

You could also use an array to achieve the same effect.
Code:
arr=( $(iax2 show netstat) )
echo $(( ${arr[1]} + ${arr[8]} ))
(Arrays begin with zero, so the numbers are shifted.)

Last edited by David the H.; 09-03-2010 at 11:46 AM. Reason: addendum
 
Old 09-03-2010, 11:50 PM   #5
santius
LQ Newbie
 
Registered: Sep 2010
Location: Montevideo, Uruguay
Distribution: Ubuntu 10.04 LTS
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks David!
That demonstrate the power of bash!

Thanks a lot, really!
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple Column output for LS Scratch59 Linux - Newbie 4 12-09-2009 05:28 PM
Loop column output handband2 Programming 8 11-05-2008 03:40 PM
gnome-terminal and console column width jhwilliams Linux - Software 3 08-16-2007 10:22 PM
calculating average of a column of numbers in bash hta1984 Programming 4 12-13-2006 11:58 AM
Sorting of multi-column output rytrom Linux - Newbie 11 09-15-2003 11:31 AM


All times are GMT -5. The time now is 05:22 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration