Pull random line from a file

Björn Lindberg d95-bli.no.spam at nada.kth.se
Fri Sep 6 18:06:29 PDT 2002

Rob Park wrote:
> Alas! Kevan Shea spake thus:
> > Hell with that I can do it in one line:
> > RNDLINE=$(sed -n $(expr $RANDOM % $(grep -c --regexp=.* $FILENAME))p $FILENAME)
> This is probably better:
> RNDLINE=$(sed -n $(expr $RANDOM % $(wc -l $FILENAME))p $FILENAME)

This doesn't work as written, because wc -l prints not only the number
of lines, but also the name of the file, ie "18 filename". Here are a
few ways to print the number of lines in a file:

	wc -l < $filename	# observe the redirection

	sed -n '$=' $filename

	awk 'END{ print NR}' $filename

Another improvement would be to use the builtin shell arithmetic instead
of expr, if you use a modern shell like bash or ksh. Like this:

RNDLINE=$(sed -n $((RANDOM % $(wc -l < $FILENAME)))p $FILENAME)

I think Richard's one-pass awk solution is the most elegant one, but not
necessarily the fastest. He has to generate a new random number for each
line, and that might take longer than simply doing two passes through
the file. I would guess that wc -l is pretty fast, although I haven't

Unsubscribe: send email to listar at linuxfromscratch.org
and put 'unsubscribe lfs-chat' in the subject header of the message

More information about the lfs-chat mailing list