Pull random line from a file

Conrad Newton conrad.newton at broadpark.no
Fri Sep 6 08:33:01 PDT 2002


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 $() notation, e.g.

RNDLINE=$(sed -n ${NUM}p $FILENAME)

has the advantage that you can embed commands
inside of other commands.  Also, people often
misread the ` notation and see it as an ordinary
quote mark  '.

Conrad

-- 
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