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 - Looking for hints on sorting a file with PowerShell

Reply
 
Old 05-29-2007   #1 (permalink)
hrh1818


 
 

Looking for hints on sorting a file with PowerShell

Say one has an ASCII file with 2000 lines and 5 comma separated
variables per line. How does one go about sorting the file in
descending order using the third variable in a line to control the
sorting order and write the sorted results to a file. The file to be
sorted can not be modified.

Thanks in advance for your hints, Howard


My System SpecsSystem Spec
Old 05-30-2007   #2 (permalink)
Keith Hill [MVP]


 
 

Re: Looking for hints on sorting a file with PowerShell

"hrh1818" <howardrh@westerncom.net> wrote in message
news:1180484139.843151.96200@q69g2000hsb.googlegroups.com...
> Say one has an ASCII file with 2000 lines and 5 comma separated
> variables per line. How does one go about sorting the file in
> descending order using the third variable in a line to control the
> sorting order and write the sorted results to a file. The file to be
> sorted can not be modified.


Pretty simply:

gc foo.txt | sort {$_.split(',')[2]} > bar.txt

This could certainly be embellished to be more fault tolerant.

--
Keith

My System SpecsSystem Spec
Old 05-30-2007   #3 (permalink)
Keith Hill [MVP]


 
 

Re: Looking for hints on sorting a file with PowerShell


"Keith Hill [MVP]" <r_keith_hill@mailhot.nospamIdotcom> wrote in message
news:582AD6C1-9B7B-463E-AE9D-9FA4C53BC03F@microsoft.com...
> "hrh1818" <howardrh@westerncom.net> wrote in message
> news:1180484139.843151.96200@q69g2000hsb.googlegroups.com...
>> Say one has an ASCII file with 2000 lines and 5 comma separated
>> variables per line. How does one go about sorting the file in
>> descending order using the third variable in a line to control the
>> sorting order and write the sorted results to a file. The file to be
>> sorted can not be modified.

>
> Pretty simply:
>
> gc foo.txt | sort {$_.split(',')[2]} > bar.txt


Missed the descending order, so that would be:

gc foo.txt | sort {$_.split(',')[2]} -desc > foo_sorted.txt

--
Keith

My System SpecsSystem Spec
Old 05-30-2007   #4 (permalink)
hrh1818


 
 

Re: Looking for hints on sorting a file with PowerShell

On May 29, 11:19 pm, "Keith Hill [MVP]"
<r_keith_h...@mailhot.nospamIdotcom> wrote:
> "Keith Hill [MVP]" <r_keith_h...@mailhot.nospamIdotcom> wrote in messagenews:582AD6C1-9B7B-463E-AE9D-9FA4C53BC03F@microsoft.com...
>


> > Pretty simply:

>
> > gc foo.txt | sort {$_.split(',')[2]} > bar.txt

>
> Missed the descending order, so that would be:
>
> gc foo.txt | sort {$_.split(',')[2]} -desc > foo_sorted.txt
>
> --
> Keith



Keith, Thank you for your reply. I am amazed with how much can be
done with one line of code in PowerShell

Howard

My System SpecsSystem Spec
Old 05-30-2007   #5 (permalink)
klumsy@xtra.co.nz


 
 

Re: Looking for hints on sorting a file with PowerShell

On May 29, 9:19 pm, "Keith Hill [MVP]"
<r_keith_h...@mailhot.nospamIdotcom> wrote:
> "Keith Hill [MVP]" <r_keith_h...@mailhot.nospamIdotcom> wrote in messagenews:582AD6C1-9B7B-463E-AE9D-9FA4C53BC03F@microsoft.com...
>
> > "hrh1818" <howar...@westerncom.net> wrote in message
> >news:1180484139.843151.96200@q69g2000hsb.googlegroups.com...
> >> Say one has an ASCII file with 2000 lines and 5 comma separated
> >> variables per line. How does one go about sorting the file in
> >> descending order using the third variable in a line to control the
> >> sorting order and write the sorted results to a file. The file to be
> >> sorted can not be modified.

>
> > Pretty simply:

>
> > gc foo.txt | sort {$_.split(',')[2]} > bar.txt

>
> Missed the descending order, so that would be:
>
> gc foo.txt | sort {$_.split(',')[2]} -desc > foo_sorted.txt
>
> --
> Keith


thats a sweet one liner, can i use it and write up about it in my one-
liner of the week column?

My System SpecsSystem Spec
Old 05-30-2007   #6 (permalink)
Keith Hill [MVP]


 
 

Re: Looking for hints on sorting a file with PowerShell

<klumsy@xtra.co.nz> wrote in message
news:1180548660.926751.17460@q75g2000hsh.googlegroups.com...
> On May 29, 9:19 pm, "Keith Hill [MVP]"
>>
>> Missed the descending order, so that would be:
>>
>> gc foo.txt | sort {$_.split(',')[2]} -desc > foo_sorted.txt
>>
>> --
>> Keith

>
> thats a sweet one liner, can i use it and write up about it in my one-
> liner of the week column?


Go for it. :-)

--
Keith

My System SpecsSystem Spec
Old 05-30-2007   #7 (permalink)
Marcel J. Ortiz [MSFT]


 
 

Re: Looking for hints on sorting a file with PowerShell

Nice! that's much better than the first thing I though of: Import-csv,
followed by sort which would have left me with the work turning it back into
lines.

"Keith Hill [MVP]" <r_keith_hill@mailhot.nospamIdotcom> wrote in message
news:emRZkbuoHHA.3264@TK2MSFTNGP04.phx.gbl...
> <klumsy@xtra.co.nz> wrote in message
> news:1180548660.926751.17460@q75g2000hsh.googlegroups.com...
>> On May 29, 9:19 pm, "Keith Hill [MVP]"
>>>
>>> Missed the descending order, so that would be:
>>>
>>> gc foo.txt | sort {$_.split(',')[2]} -desc > foo_sorted.txt
>>>
>>> --
>>> Keith

>>
>> thats a sweet one liner, can i use it and write up about it in my one-
>> liner of the week column?

>
> Go for it. :-)
>
> --
> Keith


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Sorting by file names PowerShell
File view sorting Vista file management
Sorting by file name Vista file management
File sorting in folders, how to NOT? Vista General
Something wrong with Vista networking? Some hints about enablingping and file sharing on local network Vista networking & sharing


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