Browse Source

Add stats.sh for simple stats functions

Alois Mahdal 9 years ago
parent
commit
560dea5a31
1 changed files with 43 additions and 0 deletions
  1. 43
    0
      include/stats.sh

+ 43
- 0
include/stats.sh View File

@@ -0,0 +1,43 @@
1
+#!/bin/bash
2
+
3
+
4
+average() {
5
+    #
6
+    # Get the average from numbers (one number per line)
7
+    #
8
+    perl -e '
9
+        my $cnt = $sum = 0;
10
+        while (<>) {
11
+            next unless /\d+/;
12
+            $cnt += 1;
13
+            $sum += $_;
14
+        }
15
+        print $sum / $cnt;
16
+        print "\n";
17
+    '
18
+}
19
+
20
+
21
+trim() {
22
+    #
23
+    # Remove statistical error by trimming
24
+    #
25
+    # sort numbers and throw away $1 % of biggest
26
+    # and same no. of smallest items
27
+    #
28
+    local pct=${1:-10}
29
+    local tmp=$(mktemp)
30
+    cat > $tmp
31
+    local c_all=$(cat $tmp | wc -l)
32
+    debug -v c_all pct
33
+    set -x
34
+    local offset=$(( ( c_all * $pct ) / 100 ))
35
+    set +x
36
+    debug -v offset
37
+    cat $tmp \
38
+        | sort -g \
39
+        | head -n -$offset \
40
+        | tail -n +$((offset + 1))
41
+    rm -f $tmp
42
+}
43
+