The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 relle Shell Programming and Scripting 6 07-11-2008 05:59 AM
how to create random no between 10 to 40 in C useless79 High Level Programming 5 08-19-2007 08:51 PM
Random keyvan Shell Programming and Scripting 8 05-24-2007 12:11 PM
Getting a random file davidY Shell Programming and Scripting 6 01-01-2007 05:03 AM
random in ksh pascalbout AIX 1 01-04-2006 10:53 AM

Closed Thread
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
  #1 (permalink)  
Old 05-21-2008
melaz melaz is offline
Registered User
  
 

Join Date: May 2008
Posts: 1
Random command

I am trying to select one random word from a file, any ideas on how to do this as i have only manged to generete the random number?
  #2 (permalink)  
Old 07-10-2008
milhan milhan is offline
Registered User
  
 

Join Date: Oct 2002
Location: /home
Posts: 133
Code:
#!/usr/bin/ksh

# random_word.sh: selects one random word from a file
# usage: random_word.sh file_name

file_name=$1  # $file_name is the name of file that you select a random word
num_words=`wc -w $file_name | cut -d " " -f 1`

#echo $RANDOM
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
     printf "$word"
     exit 0
  fi
done
Closed Thread

Bookmarks

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 09:04 PM.


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