[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Needing a random number generator for scripting



On Thu, Sep 27, 2001 at 08:45:57PM -0500, Jeremy Whetzel wrote:
> 
> I'm in the process of writing up a script that I need to be able to
> "randomly" switch around the lines in a text file.  (ala a random mp3
> playlist) Would anyone have any suggestions?  I was originally thinking
> of using a random number generator for it, but if there's a tool that
> would work better...?
> 
Here's what I use:

awk 'BEGIN {srand()} {print rand() "\t" $0 }' unshuffled.m3u \
 | sort | cut -f2 > shuffled.m3u

The way this works is that the awk command prepends a random number to
each line and pipes the lines into sort which sorts by the random
numbers. Next, cut is used to remove the random numbers and you get
shuffled output.

HTH

dt.

-- 
Dave Thayer           | If trees could scream, would we be so cavalier about
Denver, Colorado USA  | cutting them down? We might, if they screamed all
dave@thayer-boyle.com | the time, for no good reason. - Jack Handey



Reply to: