Skip to content

Instantly share code, notes, and snippets.

@unhammer
Created February 12, 2014 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unhammer/8955298 to your computer and use it in GitHub Desktop.
Save unhammer/8955298 to your computer and use it in GitHub Desktop.
For every PERIOD lines, output a random line from that set of lines
#!/bin/bash
set -e -u
if [[ $# -ne 1 ]];then
echo "Usage: $0 PERIOD"
echo "For every PERIOD lines, output a random line from that set of lines"
echo "E.g. printf '%s\n' {1..30} | $0 7"
echo "might output 5, 12, 19, 23 and 30."
exit 1
fi
gawk -vseed=$RANDOM -vperiod=$1 '
BEGIN{
srand(seed)
}
NR%period == 1 {
mod=int(period*rand())
}
NR%period == mod {
print
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment