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 relle Shell Programming and Scripting 6 07-11-2008 06:59 AM
FTP random files whegra Shell Programming and Scripting 3 11-21-2007 05:22 PM
Random keyvan Shell Programming and Scripting 8 05-24-2007 01: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 07-03-2008
Kweekwom Kweekwom is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 28
Help with AWK and RANDOM please

Hi, here's what I'm trying to do,

I used awk to separate numbers from a file into a list like this:

4536
23426
452
7647
35637
35635
35653

Now I need to randomly select one of this numbers, and make it a variable.

Any ideas on how to do this??

Thank you!
  #2 (permalink)  
Old 07-03-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,330
If you have the list in an array in awk you can use the rand() function:


Code:
index=int(rand()*NumberOfElements+1)
print array[index]

Regards
  #3 (permalink)  
Old 07-03-2008
Kweekwom Kweekwom is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 28
Quote:
Originally Posted by Franklin52 View Post
If you have the list in an array in awk you can use the rand() function:


Code:
index=int(rand()*NumberOfElements+1)
print array[index]

Regards
Thanks! Will this only select one of the numbers from the list?
  #4 (permalink)  
Old 07-03-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,330
Quote:
Originally Posted by Kweekwom View Post
Thanks! Will this only select one of the numbers from the list?
Yes, but the best way to get the answer is to try it out for yourself.....

Regards
  #5 (permalink)  
Old 07-07-2008
Kweekwom Kweekwom is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 28
Quote:
Originally Posted by Franklin52 View Post
Yes, but the best way to get the answer is to try it out for yourself.....

Regards
I would if I knew how

This may be a dumb question, but how do I get the list in an array?

Thanks.
  #6 (permalink)  
Old 07-07-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,330
This is an example how to fill an array "arr" with the value of the first field of a file and print a random element of the array.

Code:
awk '
{arr[++i]=$1}
END{srand()
element=int(rand()*i+1)
print arr[element]
}' file

Regards
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:22 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