Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Show a random message at bash shell startup' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Show a random message at bash shell startup
Authored by: lras on May 14, '07 10:31:20AM
... or a little shorter (to save the environment :-)
fn=/path/to/quotes.txt 
sed -n $(awk "END{ print $RANDOM%NR+1}" $fn)p $fn


[ Reply to This | # ]
No need for sed
Authored by: müzso on May 14, '07 11:32:51PM
You can go even more environment friendlier, than that ... :-) No need for sed if you already start awk ... and no need to read the file twice.
awk "{ lines[NR] = $0 } END { print lines[$RANDOM % NR + 1] }" /path/to/quotes.txt

And if you're interested in various other solutions (eg. Perl :-) ), then check out this thread.

[ Reply to This | # ]
No need for sed
Authored by: müzso on May 14, '07 11:37:31PM

Of course one should choose the solution that suites her/him better ... eg. if you've got a quotes.txt of some GB in size, then caching the lines in an array might make your os swap a little bit ... ;-)



[ Reply to This | # ]