Random word from a flat text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Random word from a flat text file
# 1  
Old 06-09-2009
MySQL Random word from a flat text file

Hello,
I need to take a random word from a flat text file with words in it seperated by spaces.

The code I am using, always gives me the first word. Can anyone please shed some light on this. Here's my code.

Thanks

echo table roof ceiling jar computer monitor keyboard carpet > wordfile

file_name=wordfile # $file_name is the name of file that you select a random word
num_words=`wc -w $file_name | cut -d " " -f 1`
rand_word=`expr "$RANDOM" % $num_words + 1`
i=0
for word in `cat $file_name`
do
i=`expr $i + 1`
if [ $i = $rand_word ]
then
echo "$word" > hidd
fi
# 2  
Old 06-09-2009
where is $RANDOM defined?
# 3  
Old 06-09-2009
How do i define $RANDOM ?
# 4  
Old 06-09-2009
try this

file_name=wordfile

num_words=`wc -w < $file_name`

rand_word=`expr "$RANDOM" % $num_words + 1`

cat $file_name | tr "\n" " " | awk -v randword=$rand_word '{print $randword}'
# 5  
Old 06-09-2009
if you have Python, here's an easy way to do it
Code:
#!/usr/bin/env python
import random
data=open("file").read().split()
print random.sample(data,1)[0]

# 6  
Old 06-09-2009
$RANDOM doesn't need to be defined. It's a shell function, like $PWD.
I didn't go through the OP script, but here's a shorter version using arrays in korn shell:

Code:
 # cat random_script.ksh
set -A WORDS $(cat wordfile)
RAN_NUM=$(echo ${WORDS[@]} | wc -w)
echo ${WORDS[$(expr "$RANDOM" % ${RAN_NUM})]}

There's probably a shorter way using AWK.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

2. Shell Programming and Scripting

gawk script to search and replace text in a flat file

Hi I am new to unix and newbie to this forum. I need help in writing a gawk script that search and replace particular text in a flat file. Input file text : ZIDE_CONTROL000 100000000003869920900000300000001ISYNC 000002225489 0000000002232122 20120321 16:40:53 ZIDE_RECORD000... (5 Replies)
Discussion started by: gkausmel
5 Replies

3. UNIX for Dummies Questions & Answers

How to del word by random input?

Hi, I got big problem. a line contains like this: Hell "A,B,C", how to delete A or B or C by using sed or other tools? I do not have any idea. Many thanks. (2 Replies)
Discussion started by: lemon_06
2 Replies

4. Shell Programming and Scripting

Random word generation with AWK

Hi - I have a word GTTCAGAGTTCTACAGTCCGACGAT I need to extract all the possible "chunks" of 7 or above letter "words" from this. SO, my out put should be GTTCAGA TTCAGAG TCAGAGT CAGAGTTCT TCCGACGAT CAGTCCGACG etc. How can I do that with awk or any other language? I have no... (2 Replies)
Discussion started by: polsum
2 Replies

5. Shell Programming and Scripting

Read random line from a text file

I have a text file with hundreds of lines, i wish to run a script and reads a random line to pass it to another command line such as: for line in `cat file |grep random line`; do echo $line |mail my@example.com ; done thank you (6 Replies)
Discussion started by: Bashar
6 Replies

6. Shell Programming and Scripting

random select text file ranamed

i want to need script.. source.txt /home/user1/public_html/test3 /home/user90/public_html/test9 . . . /home/user650/public_html/test000 read source.txt and cd /home/user**/public_html/*** and there is 1.txt, 2txt ~~25.txt and select 6 text files randomly among the... (4 Replies)
Discussion started by: topic32428285
4 Replies

7. UNIX for Dummies Questions & Answers

Retrieving random numbers out of a text file

Hi one and all, I'm working on a Bash script that is designed to calculate how much IP traffic has passed through a port to determine traffic volume over a given amount of time. I've currently been able to use the netstat -s command coupled with grep to write to a file the total packets... (13 Replies)
Discussion started by: nistleloy
13 Replies

8. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies

9. UNIX for Dummies Questions & Answers

Creating flat text file (ASCII)

Hi everybody. I need help and I hope someone is willing to help me out here. My wholesale company is currently moving to new software. The old software is running on a UNIX platform. We need to migrate data from the UNIX system, but our former software provider refuses to assist the data... (5 Replies)
Discussion started by: Wdonero
5 Replies

10. Shell Programming and Scripting

how to get rid of blank line in a flat text file

Hi, I have a flat text file which contains blank line between each text line. Is there any command to get rid of it? Thanks for your help (11 Replies)
Discussion started by: xfang
11 Replies
Login or Register to Ask a Question