Go Premium for a chance to win a PS4. Enter to Win


On Unix or Windows2K platform, need a random line sampler

Posted on 2002-04-02
Medium Priority
Last Modified: 2013-12-13
I have a 2000+ line text file and need to randomly select 250 lines from the file.

Any STANDARD or FREEWARE/DOWNLOADABLE tools to do this either in the Unix or Windows environments?

I have Unix (Compaq Tru64) or Windows (Win2K) available to do it on.  I've also got OpenVMS but I don't expect
that there's anybody here in Expert's Exchange who can answer questions on that venerable OS... if there are,
then fine, on there too -- since that's where I NEED the resultant text file of 250 lines.

Time dependent; have to have this file before Monday.
Question by:jlw011597
  • 2
  • 2
  • 2
  • +3
LVL 22

Expert Comment

ID: 6914135
Well, Perl does have a randomizer, so:

1. Make a sorted array X of 250 random, unique integer between 1 and the number of lines in the file

2. Make pass through file, selecting the lines specified by X

I'll post some code in a day or two, if nothing better shows up or if nobody else has the time to do it.

Expert Comment

ID: 6914271
PERL runs on both Unice and Windows (ActivePerl is best there)
LVL 24

Expert Comment

ID: 6914307
Note that Perl can be useful as cgi or as means to understand how to utilize underlying OS kernel features via scripts.

I often find O'Reilly as a useful resource. For example:

Sample ReadMe:
(filename: Crypt-Random-1.11.tar.gz)

Has numerous code samples for
-- you can get Windoze versions of needed code from them (yes, even freeware, open source, all them there neat fun words)

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 11

Expert Comment

ID: 6914311
250 consecutive lines or random lines?
Are there duplicates allowed?


Author Comment

ID: 6914412
From: griessh
                     250 consecutive lines or random lines?
                     Are there duplicates allowed?

Random lines, duplicates not allowed.  They're email addresses from a sample
population for a research study.  By choosing a random subset from a large population
the researchers hope to defuse complaints of SPAM by the selected individuals.

From: Others....    all suggested PERL.

          Ah, well... I don't subscribe to the Unix build-it-yourself school so was hoping
          for some actual application that did this.  But the researchers, when told it was
          a stumbling block, found a website (www.randomizer.org, I think) and had it
          supply a set of 250 random, no duplicates numbers in the entire set of 2000+
          records, and a 3rd party inserted the 2000+ records into an EXCEL spreadsheet,
          then did a query selecting the rows that matched the 250 random numbers.
So, done.  And delivered back to me via Email to my OpenVMS system where the
resultant file becomes the restricted access mailing list for sending the study request out to those 250 randomly selected members of the 2000+ member population.

LVL 11

Expert Comment

ID: 6914522

Great! I suggest to go to Community Support at http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt , post a request (with this URL included) and ask them to PAQ this question and refund your points since you have your own solution.

LVL 24

Expert Comment

ID: 6914750
qBasic can do rather quickly. It was included with NT4 'Server'!

Interesting, the no-duplicate. Either pre or post process one presumes.

I disagree with cookre on the array.. IMO the random numbers should come one at a time, no new number until the prior record processed. This means, of course, that a MUST disagree with randomizer, having prebuilt a list of numbers prior to runtime. What has 'appearance' of random, actually is not.

But since you are happy, I assume for your purpose it'll reduce your working set satisfactorily.

> the researchers hope

IMO, One unsolicited memo can be research, all subsequent having no opt-in are eSpam. Label it anything they like, if it looks like a duck, walks like a duck.....

> Time dependent; have to have this file before Monday.

hmm, probably a good editor would do as well, just delete lines, at random, until only 250 left... doesn't take all that long. I can delete faster than type.
LVL 22

Expert Comment

ID: 6915638
SunBow, the array IS built at runtime with a seed based on time so one doesn't always get the same sequence.  Moreover, the main purpose of the array is to keep track of those numbers already selected so they are not selected again.

jtw, griessh's suggesting is ideal, since the link to randomizer.org will likely be of value to others in the future.

(whew, now I don't have to actually write anything...)

Accepted Solution

Moondancer earned 0 total points
ID: 6915800
Per your request in Community Support today, I have refunded your 100 points to you for this question and closing it by moving it to our PAQ at zero points.  The PAQ is our Previously Asked Question database.

http://www.experts-exchange.com/commspt/Q.20284482.html  In your request you stated that you found your own solution and will cut/paste it below.

Please make this so.  If the points can't be refunded, then assign them to griessh -- while he didn't solve the problem, he's the one that suggests putting this into PAQ status so others can find it.  Another comment from
cookre says that his suggestion would help future users because the link supplied to www.randomizer.org will likely be of value to others.  Makes me think one of them, probably griessh, deserves the points.

Obviously if I select my OWN comment with the www.randomizer.org linkage in it as the accepted answer your system's going to balk, and if I select griessh's comment quoted above as the answer, to assign the points to him myself, then the comments containing www.randomizer.org won't be so noticable to subsequent searchers. So we need to have MY comment citing the randomizer site and the way we used it as the answer, but assign the points elsewise.  This make sense? --

You can now post a question in this same Topic Area to award points and entitle it Points for __expertname__ and in comments please paste the URL (Link) to this question. The expert will then add comments or propose an answer which you, in turn, accept to grade and close.

More information for you in the Community Support link.

Moondancer - EE Moderator


Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses

963 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