Pull random line from a file

Rob Park rbpark at ualberta.ca
Thu Sep 5 19:57:58 PDT 2002


Alas! Kevan Shea spake thus:
> 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?

This ought to do it:

rand($.) < 1 && ($line = $_) while <>;

... OH, you wanted BASH? heh, oops ;)

-- 
Rob 'Feztaa' Park
http://members.shaw.ca/feztaa/
--
"Show business is just like high school, except you get paid."
- Martin Mull
-- 
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