The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
random select text file ranamed topic32428285 Shell Programming and Scripting 4 04-29-2009 02:52 PM
Retrieving random numbers out of a text file nistleloy UNIX for Dummies Questions & Answers 13 12-14-2008 04:28 PM
Can a shell script pull the first word (or nth word) off each line of a text file? tricky Shell Programming and Scripting 5 08-17-2006 07:29 AM
Creating flat text file (ASCII) Wdonero UNIX for Dummies Questions & Answers 5 04-20-2005 06:33 AM
how to get rid of blank line in a flat text file xfang Shell Programming and Scripting 11 05-28-2003 04:08 PM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 06-09-2009
Freakhan Freakhan is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 10
Thumbs up 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
 

Bookmarks

Tags
random words

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:31 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0