[NTLUG:Discuss] Bash script question

Chris Cox cjcox at acm.org
Wed Jan 19 15:00:45 CST 2005


Lance Simmons wrote:
> Let's say I have 100 names in a file.  I want to randomly select a name
> from the file, add it to a list, and repeat that process 40 times.  The
> result of the process should be a list of 40 names in random order, some
> of which may repeat in the list.
> 
> What I've been doing up to now is putting the 100 names in random order
> and then taking the first 40 lines of the resulting list.  But I want to
> have the possibility of a name appearing multiple times on the same
> list.
> 
> Does anyone have any suggestions?
> 

Off the top of my head (this is bash)

file="$1"
# Cheep seeding (good enough?)
RANDOM=$(expr $(date +%d%S%M%S) % 32767)
for (( i=1; i <= 40; i++)); do
	s=$(expr $RANDOM % 100)
	sed -n "${s}p" $file
done



More information about the Discuss mailing list