Pull random line from a file

Richard Lightman richard at nezumi.plus.com
Fri Sep 6 09:05:40 PDT 2002


* Kevan Shea <biffcool at zombieworld.com> [2002-09-06 16:35]:
> 
> For completeness heres the end script:
> 
> NUMLINES=`grep -c --regexp=.* $FILENAME`
> NUM=`expr $RANDOM % $NUMLINES + 1`
> RNDLINE=`sed -n ${NUM}p $FILENAME`
> 
Why read the whole file twice?

awk 'rand()*NR<=1 {l=$0} END {print l}' $FILENAME

Richard


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