PageRenderTime 18ms CodeModel.GetById 15ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 1ms

/stats.sh

http://gsbabil.googlecode.com/
Shell | 60 lines | 26 code | 6 blank | 28 comment | 2 complexity | f2528ec47306d3f97d4dfdbac2ebc8cf MD5 | raw file
 1#!/bin/bash
 2
 3#
 4# [AUTHOR]  : Babil (Golam Sarwar)
 5# [VERSION] : 0.1
 6#
 7# [DOCUMENTATION]: 
 8#
 9# This scripts shows min, max, average and standard
10# deviation from a specified column of a specified 
11# file. For example, if 'file.dat' contains:
12#
13# 1 3
14# 2 9
15# 3 7
16# 4 5
17# 5 10
18#
19# $ stats.sh file.dat 2
20#
21# [*] input      : file.dat
22# [*] column     : 2
23# [*] no of data : 5
24#
25# [*] avg        : 6.8
26# [*] min        : 3
27# [*] max        : 10
28# [*] std.dev    : 2.56125
29#
30
31function usage()
32{
33	echo
34	echo "[*] usage: `basename $0` filename column"
35	echo
36	exit
37}
38
39if [ -z $1 ] || [ -z $2 ]
40then
41	usage
42fi
43
44line=`cat $1 | wc -l`
45max=`cat $1 | awk -v c=$2 '{if($c>=s){s=$c;line=NR}}END{print s}'`
46min=`cat $1 | awk -v s=9999999999 -v c=$2 '{if($c<=s){s=$c;line=NR};}END{print s}'`
47avg=`cat $1 | awk -v c=$2 '{total=total+$c}END{print total/NR}'`
48stdev=`cat $1 | awk -v avg=$avg -v c=$2 '{x=$c-avg;total=total+x*x;}END{print sqrt(total/NR)}'`
49
50echo
51echo "[*] input      : $1"
52echo "[*] column     : $2"
53echo "[*] no of data : $line"
54echo
55echo "[*] avg        : $avg"
56echo "[*] min        : $min"
57echo "[*] max        : $max"
58echo "[*] std.dev    : $stdev"
59echo
60