Solved

Pick a random word from a file UNIX

Posted on 2007-11-22
10
885 Views
Last Modified: 2008-02-01
nice easy question for u smart lot how do u pick a random word from a file that is in the formart of

word1
word2
word3
word4
word5
?
0
Comment
Question by:DSwiftie
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 4

Expert Comment

by:Dozer42
ID: 20337004
perl -e 'open IN, "<FILENAME";rand($.) < 1 && ($n=$_) while <IN>;print $n'
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20337030
#!/usr/bin/bash
file=/path/to/file
lines=`wc -l <$file
n=$RANDOM
let "n %= $lines"
sed -n ${n}p $file
0
 
LVL 84

Expert Comment

by:ozo
ID: 20337096
perl -ne '$n=$_ if rand $.<1;END{print $n}' filename
perl -MTie::File -le 'tie @a,"Tie::File", "filename"; print @a[rand @a]'
awk 'rand<1/NR{n=$0}END{print n}' < filename
perl -e '@a=<>;print$a[rand@a]' filename


0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:DSwiftie
ID: 20338073
i know using sed or perlor awk is well worth knowin but tryn to avoid using those tools
0
 
LVL 84

Expert Comment

by:ozo
ID: 20338100
What tools would you prefer to use?
0
 
LVL 84

Accepted Solution

by:
ozo earned 125 total points
ID: 20338159
#!/usr/bin/bash
count=0
while read line ;  do  
 if [[  $(( RANDOM % ++count )) = 0 ]] ; then p=$line ; fi
done < filename
echo $p
0
 

Author Comment

by:DSwiftie
ID: 20340910
ozo thanx again m8 allways spot on. Thankyou all for the help well apriaciated
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20341956
On large files, ozo's solution will be *much* slower than my bash solution.
0
 
LVL 84

Expert Comment

by:ozo
ID: 20342316
It's slower by about the same factor on small files too.

But there seemed to be an inexplicable desire to avoid sed.
We could have used head|tail instead, but  I didn't know if those were to be avoided too.
We could have implemented the sed function in pure bash, which would have simplified the while loop slightly, but that could still leave the wc
reimplementing wc in pure bash could mean two loops, and it seemed simpler to do it in one
even if there is a performance penalty, which might depend on the implementation.


0
 

Author Comment

by:DSwiftie
ID: 20343619
i can't explain sed thats y i dont use it doing tutorial on using sed and awk now but for now can't xplain myself when using it so am tryin to keep everything at a level i can understand.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
NTP Server in VMware 5 230
Most secure Linux or x86 Unix that are least prone to ransomware/malware 24 115
Unix Question 19 50
centos commands 6 54
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question