The little things give you away... A collection of various small helper stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

15 lines
542 B

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