Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

reading a big file line by line to be "out of memory" safe

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 08-01-2007   #1 (permalink)
freddy chavez
Guest


 

reading a big file line by line to be "out of memory" safe

I have a powershell script that needs to read a big file and I want to make
sure not to be "out of memory" so I thought three ways to read the file line
by line:

(1) $f = get-content bigfile.txt; $f | foreach { ...}
(2) get-content bigfile.txt | foreach {...}
(3) get-content bigfile.txt | foreach-object {...}
(4) $f = openfile(bigfile); while ($line = readline(f$)) do {...};
closefile($f)

(Yes, I know that (4) is syntactically incorrect, I show it just to make my
point)

I'm definitively sure that (1) will read all the file at one chunk.
But how about the (2) and (3)?
The (4) is the way traditional programming languages do it and it reads line
by line.
Is there any way to implement (4) in PowerShell?

Any help would be appreciated. Thanks.

Regards,
Freddy Chavez.



My System SpecsSystem Spec
Old 08-01-2007   #2 (permalink)
Shay Levi
Guest


 

Re: reading a big file line by line to be "out of memory" safe

You can always measures the time it takes to run script blocks and cmdlets
or script files
by using measure-command

Type "help Measure-Command -examples" to get some examples.

As for the performance, I'd choose option 3. It will pipe line by line instead
of reading
the whole file and than start to pipe (option 1,2)


Shay
http://scriptolog.blogspot.com



> I have a powershell script that needs to read a big file and I want to
> make sure not to be "out of memory" so I thought three ways to read
> the file line by line:
>
> (1) $f = get-content bigfile.txt; $f | foreach { ...}
> (2) get-content bigfile.txt | foreach {...}
> (3) get-content bigfile.txt | foreach-object {...}
> (4) $f = openfile(bigfile); while ($line = readline(f$)) do {...};
> closefile($f)
> (Yes, I know that (4) is syntactically incorrect, I show it just to
> make my point)
>
> I'm definitively sure that (1) will read all the file at one chunk.
> But how about the (2) and (3)?
> The (4) is the way traditional programming languages do it and it
> reads line
> by line.
> Is there any way to implement (4) in PowerShell?
> Any help would be appreciated. Thanks.
>
> Regards,
> Freddy Chavez.



My System SpecsSystem Spec
Old 08-01-2007   #3 (permalink)
Jacques Barathon [MS]
Guest


 

Re: reading a big file line by line to be "out of memory" safe

"freddy chavez" <freddychavez22@hotmail.com> wrote in message
news:%23atheAE1HHA.4652@TK2MSFTNGP05.phx.gbl...
>I have a powershell script that needs to read a big file and I want to make
>sure not to be "out of memory" so I thought three ways to read the file
>line by line:
>
> (1) $f = get-content bigfile.txt; $f | foreach { ...}
> (2) get-content bigfile.txt | foreach {...}
> (3) get-content bigfile.txt | foreach-object {...}
> (4) $f = openfile(bigfile); while ($line = readline(f$)) do {...};
> closefile($f)

....
> I'm definitively sure that (1) will read all the file at one chunk.
> But how about the (2) and (3)?


(2) and (3) are identical: in (2) foreach is an alias of foreach-object.

Performance and memory-wise I would recommend that option: get-content will
read your file one line at a time and will pass the line to your foreach
block. Depending on your actual script the performance may vary but you will
likely optimise the usage of your system's memory.

> The (4) is the way traditional programming languages do it and it reads
> line by line.
> Is there any way to implement (4) in PowerShell?


You can with direct access to .NET classes:

$f = [System.IO.File]::OpenText(bigfile.txt)
while (! $f.EndOfStream) {$line = $f.ReadLine(); ...}
$f.Close()

It is another technique that should save your memory, although not super
PowerShellish :-)

Jacques

My System SpecsSystem Spec
Old 08-01-2007   #4 (permalink)
dreeschkind
Guest


 

Re: reading a big file line by line to be "out of memory" safe

"Jacques Barathon [MS]" wrote:

> You can with direct access to .NET classes:
>
> $f = [System.IO.File]::OpenText(bigfile.txt)
> while (! $f.EndOfStream) {$line = $f.ReadLine(); ...}
> $f.Close()
>
> It is another technique that should save your memory, although not super
> PowerShellish :-)


Depends on how you define "PowerShellish". ;-)
As I see it, task oriented cmdlets are just *ONE* aspect of PowerShell.
Direct access to everything that .NET offers is just *ANOTHER* very
important aspect. The same goes for native COM and WMI access. If we had only
one of these, PoSh would probably not deserve the name PowerShell. Using
..NET, COM, native apps etc. usually has big advantages in performance, so in
my opinion "PowerShellish" is using the optimal combination of all these
different possiblities to solve a specific problem.

--
greetings
dreeschkind
My System SpecsSystem Spec
Old 08-01-2007   #5 (permalink)
dreeschkind
Guest


 

RE: reading a big file line by line to be "out of memory" safe

Another very powerful way to do this is using PowerShell's switch statement
which has a -file parameter that you can use to process big files line by
line.
Type the following to get more information about the syntax:
PS> help about_switch
Bruce Payette's book "PowerShell in Action" also has some very good examples
how to use this feature.

--
greetings
dreeschkind

"freddy chavez" wrote:

> I have a powershell script that needs to read a big file and I want to make
> sure not to be "out of memory" so I thought three ways to read the file line
> by line:
>
> (1) $f = get-content bigfile.txt; $f | foreach { ...}
> (2) get-content bigfile.txt | foreach {...}
> (3) get-content bigfile.txt | foreach-object {...}
> (4) $f = openfile(bigfile); while ($line = readline(f$)) do {...};
> closefile($f)
>
> (Yes, I know that (4) is syntactically incorrect, I show it just to make my
> point)
>
> I'm definitively sure that (1) will read all the file at one chunk.
> But how about the (2) and (3)?
> The (4) is the way traditional programming languages do it and it reads line
> by line.
> Is there any way to implement (4) in PowerShell?
>
> Any help would be appreciated. Thanks.
>
> Regards,
> Freddy Chavez.
>
>
>

My System SpecsSystem Spec
Old 08-01-2007   #6 (permalink)
Kiron
Guest


 

Re: reading a big file line by line to be "out of memory" safe

Glad to see you got the book. Have you checked the type verification in the
Setter scriptblock of a scriptProperty close to the end of § 8.2.3 on p.229?

--
Kiron

My System SpecsSystem Spec
Old 08-01-2007   #7 (permalink)
dreeschkind
Guest


 

Re: reading a big file line by line to be "out of memory" safe

Actually, I'm on page 230 right now.
Why do you ask?

--
greetings
dreeschkind

"Kiron" wrote:

> Glad to see you got the book. Have you checked the type verification in the
> Setter scriptblock of a scriptProperty close to the end of § 8.2.3 on p.229?
>
> --
> Kiron
>
>

My System SpecsSystem Spec
Old 08-01-2007   #8 (permalink)
Kiron
Guest


 

Re: reading a big file line by line to be "out of memory" safe

Just thought you might have an idea about the subject, since your apparent
interest in it on your reply to klumsy@xtra.co.nz's post "Strongly typed
scriptproperties" of 6/29/2007.

--
Kiron

My System SpecsSystem Spec
Old 08-02-2007   #9 (permalink)
dreeschkind
Guest


 

Re: reading a big file line by line to be "out of memory" safe

LOL, now you got me! :-)

I completely forgot this thread.
For my mind this was like centuries ago!

But yeah, I'm still interested in a lot of stuff.

--
greetings
dreeschkind

"Kiron" wrote:

> Just thought you might have an idea about the subject, since your apparent
> interest in it on your reply to klumsy@xtra.co.nz's post "Strongly typed
> scriptproperties" of 6/29/2007.
>
> --
> Kiron
>

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Displaying Sender's "FROM" line in Junk Mail Folder sarasota Live Mail 1 08-13-2008 11:26 PM
Reading a text file and retaining line feeds ssg31415926 PowerShell 1 04-20-2008 05:41 PM
Windows Mail News subject line held in "Indexed Location" perry Vista mail 2 08-14-2007 12:35 PM
New powershell Analyzer with Suspendshell and Single Line "Immediate Mode" klumsy@xtra.co.nz PowerShell 0 09-11-2006 11:35 PM
"<SPACE> next page; <CR> next line; Q quit" not cleared in console Jon Davis PowerShell 2 05-30-2006 12:44 PM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51