Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 07-20-2006, 12:05 AM   #1
Registered: Oct 2002
Distribution: Debian 6.0.2 (squeeze)
Posts: 944

Rep: Reputation: 30
Printing random lines of a file

I've been using the remind program to print reminders for various things, and it's usually pretty straightforward to use, but I've run into something I can't figure out how to do. I'd like to be able to get a reminder that is one randomly picked from a long list, rather that something I've scheduled for that specific day. It looks like remind can call a remote program, so I guess it boils down to needing something that will take a file, pick a given number of random lines from that file, and print them out. Any ideas?
Old 07-20-2006, 12:36 AM   #2
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
I happen to have already written just such a program, which I use to pick random words from a dictionary file. It should be self explanatory and I hope it helps!

use Tie::File;
use Fcntl 'O_RDONLY';

my $dict = shift || '/usr/share/dict/words';
my $times = shift || 1;

if ($times == 1) {

  rand($.) < 1 && ($word = $_) while <DICTIONARY>;
  print $word;

} else {

  tie @f, 'Tie::File', $dict, mode => O_RDONLY
    or die "Couldn't open $dict: $!\n";
  my $size = scalar @f;
  $\ = "\n";

  while ($times--) {
    print $f[rand $size];
Old 07-20-2006, 12:42 AM   #3
Senior Member
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
a related questions is answered in perlfaq5
Old 07-20-2006, 09:00 AM   #4
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
I like the algorithm from the Camel Book.

Any interest in trying to it as a bash script? I think this might work (WARNING -- I have tested the pieces, but not the whole):


[ $1 -gt 0 ] && N=$1
[ -f $2 ]    && F=$2

C=`wc -l $F`

for ((I=1; $I<=$N; I++))
   sed -n "${L}p" $F
Mild apologies for the 1 letter variables, old habit that dies hard. (Yes, reinforced by BASIC, but not learned there.)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading random lines in c++? twirl Programming 7 10-30-2005 10:11 AM
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 02:03 PM
lexmark printing last 2 lines on seperate page polrus Linux - Hardware 1 06-14-2005 01:02 AM
Screen goes blank(random) to vertical lines bruce1271 Linux - General 2 11-15-2003 11:02 AM
Printing OpenOffice drawing without thick contour lines?? carlos123 Linux - Software 0 11-07-2003 04:01 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:04 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration