Help with AWK and RANDOM please


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with AWK and RANDOM please
# 1  
Old 07-03-2008
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  
Old 07-03-2008
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  
Old 07-03-2008
Quote:
Originally Posted by Franklin52
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  
Old 07-03-2008
Quote:
Originally Posted by Kweekwom
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.....Smilie

Regards
# 5  
Old 07-07-2008
Quote:
Originally Posted by Franklin52
Yes, but the best way to get the answer is to try it out for yourself.....Smilie

Regards
I would if I knew how Smilie

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

Thanks.
# 6  
Old 07-07-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk (or) UNIX random RGB colors generator?

Dear UNIX Friends, I was wondering if there is a random RGB color generator or any function in any unix platforms. Please share your ideas. Thanks (2 Replies)
Discussion started by: jacobs.smith
2 Replies

2. 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

3. Shell Programming and Scripting

unique random numbers awk

Hi, I have a small piece of awk code (see below) that generates random numbers. gawk -F"," 'BEGIN { srand(); for (i = 1; i <= 30; i++) printf("%s AM329_%04d\n",$0,int(36 * rand())+1) }' OFS=, AM329_hole_names.csv The code works fine and generates alphanumeric numbers like AM329_0001,... (2 Replies)
Discussion started by: theflamingmoe
2 Replies

4. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

5. 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

6. Shell Programming and Scripting

choose random text between constant string.. using awk?

Hallo I have maybe a little bit advanced request.... I need to choose one random part betwen %.... so i have this.. % text1 text1 text1 text1 text1 text1 text1 text1 text1 % text2 text2 text2 text2 text2 % text3 text3 text3 tetx3 % this choose text between % awk ' /%/... (8 Replies)
Discussion started by: sandwich
8 Replies

7. Shell Programming and Scripting

help with ksh/awk/sed script, random # of fields

Hello all, I'm working on an attendance callout script for a school district. I need to change our current layout for the vendor. Currently the data is in the form of: studentid,period,building, Heres a sample of some made up records: 500,1,30, 500,2,30, 500,3,30, 500,6,30,... (7 Replies)
Discussion started by: axo959
7 Replies

8. Shell Programming and Scripting

doing rot13 on a field with a random number with awk

i have a several million line file like this: M:charitygeneral:water:fairbanks:charitygeneral field 2 and field 5 are the same i want to read the file and rot13 or any caesar cipher field 2 and replace the with a random number 1 - 9 anyone know how to do this? something slightly... (8 Replies)
Discussion started by: bathtub
8 Replies

9. UNIX for Dummies Questions & Answers

how can i isolate the random sequence of numbers using awk?

as you can see there is a delimiter after c8 "::". Awk sees the rest as fields because it doesn't recognize spaces and tabs as delimiters. So i am basically looking to isolate 20030003ba13f6cc. Can anyone help? c8::20030003ba13f6cc disk connected configured unknown (2 Replies)
Discussion started by: rcon1
2 Replies
Login or Register to Ask a Question