Pull random line from a file

Björn Lindberg d95-bli.no.spam at nada.kth.se
Fri Sep 6 17:47:11 PDT 2002

Conrad Newton wrote:
> On Fri, Sep 06, 2002 at 10:18:39AM -0500, Kevan Shea wrote:
> > For completeness heres the end script:
> >
> > NUMLINES=`grep -c --regexp=.* $FILENAME`
> > NUM=`expr $RANDOM % $NUMLINES + 1`
> > RNDLINE=`sed -n ${NUM}p $FILENAME`
> If you find yourself doing a lot of scripting,
> you may prefer the $() notation to the backticks ``.

The only caveat is that the $() is a newer notation, originally
introduced by the Korn Shell and now also used by similar shells like
bash and zsh. The original Bourne shell only have the
backticks-notation. This can be an issue from a portability standpoint,
but if you only intend to use your script on Linux (which always has
bash) it's no problem.

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