• C

Random line from text file

Hi,

I just need to read a random line from a text file, say quotes.txt for exmaple. This is for a program I'm going to be running under Linux in the Bash shell, so I imagine using /dev/urandom or /dev/random to seed would work well enough. Also, this does not need to be an entire program, I only require a simple function for this, which would return only one line each time the function is called.

Thank you.
devolved99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
You have 2 paths for this, in my opinion:
- Non-indexed, to produce a random number, you have to know how many lines are present. To do this, you need to scan all text file counting newline characters. Then you can produce a random number. To retrieve selected line, you have to read all the file, line by line, up to reach the desired line.
- Indexed, more complex but far faster. You need to create a binary index first, where each record is the offset where first character of each line is located at text file. Number of lines is the division of index_file_size by sizeof_record (let's say 4), don't need to scan all text file to know. Chosse a random number, go to index file, obtain desired line index, go to text file and directly read line at proper location.
 

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
devolved99Author Commented:
Thanks jaime.
ozoCommented:
/* a one pass method */
FILE *f = fopen("file","r");
if( !f ){ perror("file"); exit(1); }
char in[1000],out[1000];
int line = 0;
while( fgets(rand()%++line?in:out,100,f) ){}
fputs(out,stdout);
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Kent OlsenDBACommented:

I know that I'm a late arrival, but since you're in a unix/linus environment, why not let the shell do this for you?

# Pick a line number
LINE=24

#Get the line
head -n $LINE SomeFile|tail -n 1


Kent
devolved99Author Commented:
lol,

Indeed Kent, that is what I tried doing, which worked just as well. However, in the end I chose a simple Perl script for the job.

Here is what I am now using:

#!/usr/bin/perl

for($x=0;$x<=4;$x++) {
 open(plist,"proxylist.txt") or die "$!";
  srand();
  $nlines=@file=<plist>;
  $rprox = $file[int rand $nlines];
  print "$rprox";
 close(plist);
}
Kent OlsenDBACommented:

Yeah,

Getting a random number within a range is not straight-forward in KSH.  The rest is a piece of cake.


Glad it worked out for you,
Kent



ozoCommented:
You can do it without opening proxylist.txt 4 times.
Do you want to allow the possibility of repeated lines?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.