|
- #!/usr/bin/env bash
- if [[ "$#" -eq 0 || ( "$#" -eq 2 && "$1" != '-0' ) || "$#" -gt 2 || "${@: -1}" != @(1?(.+(0))|0?(.+([0-9]))) ]]; then
- printf 'Usage: sample [-0] PROBABILITY\n' >&2
- printf 'Read lines from stdin, and print each line again with a given probability (a number between 0 and 1).\n' >&2
- printf 'With -0, use NUL instead of line feeds on both input and output.\n' >&2
- exit 1
- fi
-
- if [[ "$#" -eq 2 && "$1" == '-0' ]]; then
- awkopts=(-v 'RS=\000' -v 'ORS=\000')
- shift
- fi
-
- exec awk "${awkopts[@]}" -v "prob=$1" 'rand() <= prob'
|