Bash one liner to randomize lines in file

October 18, 2007

Discovered that the bash shell has a variable called $RANDOM, which outputs a pseudo-random number every time you call it. Sweet! Allowed me to randomize the lines in a file for a process I needed to do, thusly:

for i in cat unusual.txt; do echo “$RANDOM $i”; done | sort | sed -r ’s/1+ //’ > randorder.txt

In other words, put a random number on every line, sort the file, then take off the random numbers. Worked like a charm.


  1. 0-9 [return]
comments powered by Disqus