: ########################################################################## # Title : shuffle - print lines of a file in random order # Author : Heiner Steven # Idea from a script by Chris F.A. Johnson # Date : 2003-07-01 # Requires : nawk # Category : File Utilities # SCCS-Id. : @(#) shuffle 1.3 03/07/06 ########################################################################## # Description # Shuffles the sequence of lines in a pseudo-random order. # Each line is printed exactly once. ########################################################################## PN=`basename "$0"` # Program name VER='1.3' # We need a "new" awk having srand() and rand(). Examples: GNU awk "gawk", # Solaris "nawk", or "mawk". #NAWK=awk Usage () { echo >&2 "$PN - print lines of a file in random order, $VER usage: $PN [file ...]" exit 1 } Msg () { for MsgLine do echo "$PN: $MsgLine" >&2 done } Fatal () { Msg "$@"; exit 1; } searchprog () { _search=$1; shift for _dir in `echo "$PATH" | sed "s/^:/.:/;s/:\$/:./;s/:/ /g"` do [ -x "$_dir/$_search" ] || continue echo "$_dir/$_search" return 0 done return 1 } while [ $# -gt 0 ] do case "$1" in --) shift; break;; -h) Usage;; -*) Usage;; *) break;; # First file name esac shift done : ${NAWK:=`searchprog mawk || searchprog gawk || searchprog nawk || echo awk`} randseed=${RANDOM:-} "$NAWK" ' BEGIN { srand('"$randseed"') } # initialize random number generator { print rand() " " $0 } ' "$@" | sort -n | # Sort numerically on first column cut -f2- # Remove sorting column