• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Looking for hints on sorting a file with PowerShell

H

hrh1818

#1
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 Computer

K

Keith Hill [MVP]

#2
"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 Computer

K

Keith Hill [MVP]

#3
"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 Computer

H

hrh1818

#4
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 Computer

K

klumsy@xtra.co.nz

#5
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 Computer

K

Keith Hill [MVP]

#6
<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 Computer

M

Marcel J. Ortiz [MSFT]

#7
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 Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)