Random picking without repetition

Categories: Uncategorized

So the problem was to draw people at random from a list. The list is contained in a leads.txt text file, one per line.

This nifty one-liner will output a randomly-picked person from that file every time it’s invoked. it’ll then remove the name from the file so it doesn’t get repeated again.

export i=`sort leads.txt |shuf  |head -1` ;  echo $i; sed -i "s/^$i$//;/^$/d" leads.txt

It can be shortened by changing shuf |head 1 to shuf -h 1.

If you’d rather avoid deleting already-chosen entries from the file, this version just comments the names it picks:

export i=`sort leads.txt |grep -v \# |shuf |head -1` ;echo $i;  sed -i "s/^$i$/#$i/" leads.txt