Skip to content

Commit 576d6be

Browse files
committed
Add AWK scripts for computing basic statistics
1 parent 222c4b2 commit 576d6be

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed

tools/awk/count.awk

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

tools/awk/max.awk

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

tools/awk/mean.awk

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

tools/awk/median.awk

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

tools/awk/min.awk

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

tools/awk/sum.awk

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

0 commit comments

Comments
 (0)