Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

How to print a random line from a huge file?

This is a discussion on How to print a random line from a huge file? within the PERL forums, part of the Web Development category; Hi there, Say I have a text file of huge size, say 1G (?), and I want to print a random line from that file. The way I was thinking ...

Go Back   Geeks Talk > Web Development > PERL
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read

PERL PERL Scripts - Tips, Tricks, Issues, Latest News, PERL Resource and PERL related topics can be discussed in this forum.

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-02-2009
Junior Member
 
Join Date: Apr 2009
Location: Us
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
cji123 is on a distinguished road
How to print a random line from a huge file?

Hi there,
Say I have a text file of huge size, say 1G (?), and I want to print a random line from that file. The way I was thinking of is:

1. run a while loop where I can get the total line number of that file.
2. call the random function by giving the range of that total line number.
3. run the while loop for the second time to print out that random line.
But it is really costly! Is there a much better, more efficient way to do that.

Thanks,
Charlie,
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-17-2009
Junior Member
 
Join Date: May 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
chorny is on a distinguished road
Re: How to print a random line from a huge file?

There are several ways to do this:
1. Store data in DB.
2. Read once and index it: store somewhere start of each line.
Reply With Quote
  #3 (permalink)  
Old 07-01-2009
Contributing Member
 
Join Date: Jun 2009
Location: United States
Posts: 71
Thanks: 0
Thanked 5 Times in 4 Posts
CSOOR is on a distinguished road
Re: How to print a random line from a huge file?

Try to call unix command thro perl using system command .

You can count the line if you want. If you want print any line use sed -n command , this is simple.
Reply With Quote
Reply

  Geeks Talk > Web Development > PERL

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
how to read line by line from a file in C rohit_tayal06 C and C++ 2 07-29-2008 04:21 AM
How to match multiline when reading input file line by line vishwaRam PERL 1 02-07-2008 08:35 AM
Writing flat file line by line venkat_doda WinRunner 1 12-10-2007 06:51 AM
How to replace particular region only and print all content as a output file. vishwaRam PERL 0 10-29-2007 03:59 AM
Append the result of another shell script to the last line of a file swaroopa Unix/Linux 1 06-26-2007 05:29 PM


All times are GMT -4. The time now is 02:22 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved