LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Average of a column in console output (https://www.linuxquestions.org/questions/linux-general-1/average-of-a-column-in-console-output-830029/)

santius 09-02-2010 06:04 PM

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!

ghostdog74 09-02-2010 07:48 PM

Code:

var=$(iax2 show netstat)
set -- $var
echo $(($2 + $9))


santius 09-03-2010 11:20 AM

Quote:

Originally Posted by ghostdog74 (Post 4086532)
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...

David the H. 09-03-2010 11:43 AM

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.)

santius 09-03-2010 11:50 PM

Thanks David!
That demonstrate the power of bash!

Thanks a lot, really!


All times are GMT -5. The time now is 01:05 AM.