3
# stats: perform statistical calculations on a file or standard input
6
# Copyright 2017 Dustin Kirkland <dustin.kirkland@gmail.com>
8
# Licensed under the Apache License, Version 2.0 (the "License");
9
# you may not use this file except in compliance with the License.
10
# You may obtain a copy of the License at
12
# http://www.apache.org/licenses/LICENSE-2.0
14
# Unless required by applicable law or agreed to in writing, software
15
# distributed under the License is distributed on an "AS IS" BASIS,
16
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
# See the License for the specific language governing permissions and
18
# limitations under the License.
23
# Work from a temp file, which we'll clean up when done
25
trap "rm -f ${file}" HUP INT QUIT ILL TRAP KILL BUS TERM
27
# Handle either a file as argument, or standard input
31
cat /dev/stdin >"$file"
34
# Determine our operating mode by how we were called
37
awk -v min=0 'NR == 1 || $1 < min {line = $0; min = $1} END {print min}' "$file"
40
awk -v max=0 'NR == 1 || $1 > max {line = $0; max = $1} END {print max}' "$file"
43
awk '{ sum += $1; n++ } END { if (n > 0) print sum / n; }' "$file"
46
sort -g "$file" | awk '{ a[i++]=$1; } END { print a[int(i/2)]; }'
49
sort -g "$file" | uniq -c | sort -r -g | head -n1 | awk '{print $2}'
52
awk '{ x+=$0; y+=$0^2 } END { print sqrt(y/NR-(x/NR)^2) }' "$file"
55
sort -g "$file" | uniq -c | sort -r -g
58
awk '{ sum += $1 } END { print sum }' "$file"