What's an easy way to read random line from a file in Unix command line?

Newbie Prog picture Newbie Prog · Jan 15, 2009 · Viewed 103.3k times · Source

What's an easy way to read random line from a file in Unix command line?

Answer

unbeknown picture unbeknown · Jan 15, 2009

You can use shuf:

shuf -n 1 $FILE

There is also a utility called rl. In Debian it's in the randomize-lines package that does exactly what you want, though not available in all distros. On its home page it actually recommends the use of shuf instead (which didn't exist when it was created, I believe). shuf is part of the GNU coreutils, rl is not.

rl -c 1 $FILE