Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: random line in a very large file
Replies: 3   Last Post: Mar 9, 2011 6:57 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Ramiro

Posts: 26
Registered: 9/18/07
Re: random line in a very large file
Posted: Mar 9, 2011 6:57 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On Mar 7, 5:53 am, David Bailey <d...@removedbailey.co.uk> wrote:
> On 06/03/2011 10:44, Ramiro wrote:
>
>
>

> > Hi everyone,
>
> > I have very large files (close to 1gb). I want to find a random line
> > on it, I wanted to compare the Mathematica native commands, versus
> > calling a unix command such as sed. For example:

>
> > file = "example";
> > n = 1000000;
> > Export[file, Range[n], "List"];
> > i = RandomInteger[{1, n}];

>
> > str = OpenRead[file];
> > Skip[str, "String", i];
> > sample1 = Read[str, Expression];
> > Print[sample1];
> > Close[str];

>
> Are the lines of equal length? If they are, you could calculate where
> each line starts, and open the file in binary mode, and position using
> SetStreamPosition, then read in the correct number of bytes. Remember
> that some systems (Windows) use two bytes at the end of each line!
>
> David Baileyhttp://www.dbaileyconsultancy.co.uk


Thanks so much. Readline did the trick.




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.