MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Obtaining Random LIne from A file

  • To: mathgroup at smc.vnet.net
  • Subject: [mg129880] Re: Obtaining Random LIne from A file
  • From: Dana DeLouis <dana01 at icloud.com>
  • Date: Thu, 21 Feb 2013 05:45:41 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net

>   Block[{i = RandomInteger[{1, n}], str = OpenRead[file], res},
>    Skip[str, "String", i];
>    res = Read[str, Expression];


Hi.  I can't test this, but does this command work?

(* Set pointer to line 100 *)

SetStreamPosition[str, 100]

I'm on a Mac, and working with file locations is a little difficult for me.
I seem to remember testing this with Windows a long time ago, but I most likely am wrong.

Good Luck.  :>)

= = = = = = = = = =
HTH  :>)
Dana DeLouis
Mac & Mathematica 9
= = = = = = = = = =



On Saturday, February 16, 2013 1:07:57 AM UTC-5, Ramiro Barrantes wrote:
> Hello,
>
>
>
> I would like to get a random line from a file, I know this can be done
>
> with Mathematica but I am playing with using sed to see if it goes
>
> faster, say I want to get line 1000
>
>
>
> In mathematica it would be:
>
>
>
> <<"! sed -n p1000 filename.txt"
>
>
>
> However, I am trying to put the filename as a variable, say
>
>
>
> filename="hugefile.txt"
>
>
>
> cmd="! sed -n p1000 "<>filename
>
> <<cmd
>
>
>
> does not work.
>
>
>
> How can I do this?
>
>
>
> Lastly, I am getting a randomline using mathematica doing:
>
>
>
> getRandomLine[file_, n_] :=
>
>   Block[{i = RandomInteger[{1, n}], str = OpenRead[file], res},
>
>    Skip[str, "String", i];
>
>    res = Read[str, Expression];
>
>    Close[str];
>
>    res[[2]]
>
>    ]
>
>
>
> However, it is very slow so I was going to try with sed.Any suggestions?
>
>
>
> Thanks in advance,
>
> Ramiro




  • Prev by Date: Is there a way to integrate and differentiate Erfi?
  • Next by Date: Re: Is there a way to integrate and differentiate Erfi?
  • Previous by thread: Re: Obtaining Random LIne from A file
  • Next by thread: barchart