The Math Forum

Search All of the Math Forum:

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

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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 ]

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

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];

1) is this the most efficient way to do it in Mathematica, it's taking
very long for my purposes on my files (note, the sample file above is
small in comparison with the real data)
2) how can I call a command such as:

sed '52q;d' example

I am trying to do:

cmd= "!sed '52q;d' example"

and then

<< cmd

but it's not working. I am not sure how to use << in such a way, nor
how to mix characters such as ' and ".

Any suggestions?

Thanks in advance, any help appreciated,
p.s. by the way, the sed command above _seems_ to be faster on my
files from the command line, that is why I have the option of using

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

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.