File tree Expand file tree Collapse file tree 6 files changed +147
-0
lines changed
Expand file tree Collapse file tree 6 files changed +147
-0
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/env awk -f
2+ #
3+ # Computes the number of values.
4+ #
5+ # Usage: count
6+ #
7+ # Input:
8+ # - a column of numbers
9+ #
10+ # Output:
11+ # - number of values
12+
13+ BEGIN {
14+ count = 0
15+ }
16+ {
17+ count++
18+ }
19+ END {
20+ print count
21+ }
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env awk -f
2+ #
3+ # Computes the maximum value.
4+ #
5+ # Usage: max
6+ #
7+ # Input:
8+ # - a column of numbers
9+ #
10+ # Output:
11+ # - maximum value
12+
13+ ! i++ {
14+ # Only for the first record:
15+ max = $1
16+ }
17+ {
18+ if ( $1 > max ) {
19+ max = $1
20+ }
21+ }
22+ END {
23+ print max
24+ }
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env awk -f
2+ #
3+ # Computes the arithmetic mean.
4+ #
5+ # Usage: mean
6+ #
7+ # Input:
8+ # - a column of numbers
9+ #
10+ # Output:
11+ # - arithmetic mean
12+ #
13+ # Notes:
14+ # - Uses [Welford's method][1].
15+ #
16+ # [1]: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm
17+
18+ BEGIN {
19+ N = 0
20+ mean = 0
21+ delta = 0
22+ }
23+ {
24+ N++
25+ delta = $1 - mean
26+ mean += delta / N
27+ }
28+ END {
29+ print mean
30+ }
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env awk -f
2+ #
3+ # Computes the median.
4+ #
5+ # Usage: median
6+ #
7+ # Input:
8+ # - a column of numbers
9+ #
10+ # Output:
11+ # - median value
12+
13+ BEGIN {
14+ i = 0
15+ }
16+ {
17+ a[ i++ ] = $1
18+ }
19+ END {
20+ j = i/ 2
21+ if ( i% 2 == 1 ) {
22+ median = a[ int (j)]
23+ } else {
24+ median = (a[ j] + a[ j- 1 ] )/ 2
25+ }
26+ print median
27+ }
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env awk -f
2+ #
3+ # Computes the minimum value.
4+ #
5+ # Usage: min
6+ #
7+ # Input:
8+ # - a column of numbers
9+ #
10+ # Output:
11+ # - minimum value
12+
13+ ! i++ {
14+ # Only for the first record:
15+ min = $1
16+ }
17+ {
18+ if ( $1 < min ) {
19+ min = $1
20+ }
21+ }
22+ END {
23+ print min
24+ }
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env awk -f
2+ #
3+ # Computes the sum.
4+ #
5+ # Usage: sum
6+ #
7+ # Input:
8+ # - a column of numbers
9+ #
10+ # Output:
11+ # - sum
12+
13+ BEGIN {
14+ sum = 0
15+ }
16+ {
17+ sum += $1
18+ }
19+ END {
20+ print sum
21+ }
You can’t perform that action at this time.
0 commit comments