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