![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||