Skip to content

Commit f15d78e

Browse files
committed
Add AWK script to sample a column of values
1 parent 29fa16b commit f15d78e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tools/awk/sample.awk

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env awk -f
2+
#
3+
# Samples a column of values.
4+
#
5+
# Usage: sample prob seed
6+
#
7+
# Input:
8+
# - a column of values
9+
#
10+
# Arguments:
11+
# - prob: success probability
12+
# - seed: pseudorandom number generator seed
13+
#
14+
# Output:
15+
# - sample values
16+
#
17+
# Example:
18+
# $ sample 0.1 $RANDOM
19+
20+
BEGIN {
21+
if (ARGC == 1) {
22+
prob = 0.5
23+
} else if (ARGC == 2) {
24+
prob = ARGV[1]
25+
ARGV[1] = ""
26+
} else {
27+
prob = ARGV[1]
28+
srand(ARGV[2])
29+
ARGV[1] = ""
30+
ARGV[2] = ""
31+
}
32+
# No filenames so force stdin:
33+
ARGV[ARGC++] = "-"
34+
}
35+
rand() <= prob {
36+
print $0
37+
}

0 commit comments

Comments
 (0)