Pull random line from a file

DJ Lucas dj_me at swbell.net
Thu Sep 5 15:35:53 PDT 2002

Kevan Shea  wrote in message:

> I want to pull a random line from a file in a bash script.  I can
> generate the random number fine, but how would I go about pulling a
> single line from a file?
> I figure this is a one-line sed at worst, but I'm woefully lost by
> any sed that isn't s/find/replace/ and I can't figure what else I
> would use... fortune maybe?
> --
> Kevan Shea

Goofy idea, kinda just right off of the top of my head, but might just
work depending on how many lines your wanting. I'm not familiar with the
random function however.  Just asume it's called random with arguments
first and last (in the set) in the following example.

# Begin Random-line.sh
VariableA=$(random 1 20)
case "$VariableA" in
        VariableB='whatever line your wanting to extract'
        VariableB='whatever line you want for 2'
        VariableB='whatever line you want for 20'
Echo $VariableB
# whatever else you want to do goes here and below....etc etc
# End Random-line.sh

I'm sure there is a much more elegant way to do this however..lol  And
the syntax is probably not even close to right...take a look at
/etc/rc.d/init.d/template if you can actually use this method.

Just a rather dumb idea,


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