Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Sort text file

Reply
 
Old 01-15-2009   #1 (permalink)
Ken


 
 

Sort text file

I have a comma delimited text file with 58 columns. I am using this
script to sort the file on the first column.
Import-csv c:\temp\largeFile.txt|sort-object Col1 | out-file c:\temp
\sorted_LargeFile.txt

The output file is sorted, but it's a list rather than a table. How to
make the script output the file while retaining the tabular structure?
Example-
Original file-
Col1, col2, col3......................Col58
John, SSN, M........................some value
Alice, SSN, F........................some value

Outputted Sorted file (Notice that the sorting on the name col worked)
Col1: Alice
Col2: SSN
Col3: F
.....
Col58: some value

Col1: John
Col2: SSN
Col3: M
.....
Col58: some value

Thank you

My System SpecsSystem Spec
Old 01-15-2009   #2 (permalink)
RickB


 
 

Re: Sort text file

Use export-csv rather than out-file

Ken wrote:
Quote:

> I have a comma delimited text file with 58 columns. I am using this
> script to sort the file on the first column.
> Import-csv c:\temp\largeFile.txt|sort-object Col1 | out-file c:\temp
> \sorted_LargeFile.txt
>
> The output file is sorted, but it's a list rather than a table. How to
> make the script output the file while retaining the tabular structure?
> Example-
> Original file-
> Col1, col2, col3......................Col58
> John, SSN, M........................some value
> Alice, SSN, F........................some value
>
> Outputted Sorted file (Notice that the sorting on the name col worked)
> Col1: Alice
> Col2: SSN
> Col3: F
> ....
> Col58: some value
>
> Col1: John
> Col2: SSN
> Col3: M
> ....
> Col58: some value
>
> Thank you
My System SpecsSystem Spec
Old 01-16-2009   #3 (permalink)
Ken


 
 

Re: Sort text file

Thank you, that sorted out the formatting problem. I am sorting a file
100Mb in size and script consume 3.5GB of memory, runs for over 10
minutes. I am running this on a quad core 2 proc server. The same sort
operation on a Linix box takes un-noticable resources and less than 30
seconds to complete. Is the Powershell sort algorithm implementation
just inferior to the Unix counterpart?
import-csv <file>|sort-object -property <col name>| Export-csv <path
of sorted_file>


My System SpecsSystem Spec
Old 01-16-2009   #4 (permalink)
PaulChavez


 
 

Re: Sort text file

That's just a natural consequence of the overhead involved with creating a
bunch of custom powershell objects. Powershell offers a pretty rich
environment but one of the current downsides is you pay dearly in performance.

I've run into similar issues where the simple and obvious powershell way
just did not scale up well.

A faster way might be to use [System.IO.File]::ReadAllLines() to get a
string array and then use the sort method on that array. Since you're sorting
from the first column I *think* the sort should produce the same results.



"Ken" wrote:
Quote:

> Thank you, that sorted out the formatting problem. I am sorting a file
> 100Mb in size and script consume 3.5GB of memory, runs for over 10
> minutes. I am running this on a quad core 2 proc server. The same sort
> operation on a Linix box takes un-noticable resources and less than 30
> seconds to complete. Is the Powershell sort algorithm implementation
> just inferior to the Unix counterpart?
> import-csv <file>|sort-object -property <col name>| Export-csv <path
> of sorted_file>
>
>
>
My System SpecsSystem Spec
Old 01-16-2009   #5 (permalink)
PaulChavez


 
 

Re: Sort text file

See if this works. Assumes $inputfile contains a fileinfo object for your
source file.

$array = [System.IO.File]::ReadAllLines($inputfile.fullname)
$array[0] | Set-content "outfile.txt" #save off header
$array = $array[1..$array.length] #trim off header
[System.Array]::Sort($array)
add-Content "outfile.txt" $array




"PaulChavez" wrote:
Quote:

> That's just a natural consequence of the overhead involved with creating a
> bunch of custom powershell objects. Powershell offers a pretty rich
> environment but one of the current downsides is you pay dearly in performance.
>
> I've run into similar issues where the simple and obvious powershell way
> just did not scale up well.
>
> A faster way might be to use [System.IO.File]::ReadAllLines() to get a
> string array and then use the sort method on that array. Since you're sorting
> from the first column I *think* the sort should produce the same results.
>
>
>
> "Ken" wrote:
>
Quote:

> > Thank you, that sorted out the formatting problem. I am sorting a file
> > 100Mb in size and script consume 3.5GB of memory, runs for over 10
> > minutes. I am running this on a quad core 2 proc server. The same sort
> > operation on a Linix box takes un-noticable resources and less than 30
> > seconds to complete. Is the Powershell sort algorithm implementation
> > just inferior to the Unix counterpart?
> > import-csv <file>|sort-object -property <col name>| Export-csv <path
> > of sorted_file>
> >
> >
> >
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Convert hostname text file to IP text file VB Script
Simple sort text file PowerShell
Looping Through A Text File To Find Patterns From Another Text Fil PowerShell
Howto: Add lines of text from a specific point in a text file.. VB Script
How do I read a text file and sort text by fixed positions? PowerShell


Vista Forums 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 Ltd

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