Calculate Percentage

Hello,

Ive got a bunch of numbers here e.g:

6065
6094
6348
6297
6161
6377
6338
6290

How do I find out if there is a difference between 10% or more between one of these numbers ? I am trying to do this in Bash.. but no luck so far.. Does anyone have an Idea ??

Thanx,

  • Pascal den Bekker

Don't quite understand the objective. Could you try to explain it once more, please. An example would help.

I am trying to create a script for Icinga (Monitoring). I am getting these values from Graphite function. These numbers represent the traffic an application is getting divided on 8 nodes. I just want to make sure the traffic is equally dived on the 8 nodes.. sure I can watch on the LB, but I want to make sure the numbers from the application side are comparebly with the these from the LB.. Does someone know how to compare them ?? I tried already with awk.. didnt work so far..

I guess I didn't ask clearly enough.
Could you please provide a desired output based on the sample input provided.
Also, please show what you've tried so far.

I don't know if i have completely understand your question but i give you my answer:

min=$(cat file | awk 'BEGIN {min =1000000} {if ($0<min) min=$0} END {print min}');awk -v var="$min" '{if ($0>=(var+var*0.1))print $0} file'

The value min must be greater than the max value of your file, i fixed it to one million

Maybe this would be a good starting point:

awk '
NR == 1 {
	m = M = $1
	mn = NR
}
{	if($1 < m) {
		m = $1
		mn = NR
	}
	else if($1 > M) M = $1
	v[NR] = $1
}
END {	printf("minimum: %d, maximum: %d, difference: %.2f%%\n", 
		m, M, M * 100 / m - 100)
	for(i = 1; i <= NR; i++) 
		if(v / m > 1.1)
			printf("Node %d(%d) is %.2f%% of node %d(%d)\n",
				i, v, v * 100 / m, mn, m)
}' file

With your sample input, it produces the output:

minimum: 6065, maximum: 6377, difference: 5.14%

If we add the value 6800 to the end of your sample input, it produces:

minimum: 6065, maximum: 6800, difference: 12.12%
Node 9(6800) is 112.12% of node 1(6065)

If we change the 6800 to 5000, it produces the output:

minimum: 5000, maximum: 6377, difference: 27.54%
Node 1(6065) is 121.30% of node 9(5000)
Node 2(6094) is 121.88% of node 9(5000)
Node 3(6348) is 126.96% of node 9(5000)
Node 4(6297) is 125.94% of node 9(5000)
Node 5(6161) is 123.22% of node 9(5000)
Node 6(6377) is 127.54% of node 9(5000)
Node 7(6338) is 126.76% of node 9(5000)
Node 8(6290) is 125.80% of node 9(5000)

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk .

since your requirement is unclear
here is a simple script for mean, min and max, maybe you can calculate the percentage you want from these parameters

awk �{if(min==�"){min=max=$1}; if($1>max) {max=$1}; if($1< min) {min=$1}; total+=$1; count+=1} END {print total/count, min, max}�

senhia83,
this is not 'mean' - this is average.

1 Like

Interesting, I have to do some reading it seems.always though they are same.

Average is a generic term that, depending on context, can refer to a mean, the median, or the mode of a set of numbers. In some contexts, average and arithmetic mean are synonymous (referring to the sum of a set of numbers divided by the number of elements in the set). In addition to arithmetic mean, there are lots of other means (including, but not limited to, geometric mean, harmonic mean, interquartile mean, quadratic mean, truncated mean).

2 Likes