Windows Vista Forums

Looking for hints on sorting a file with PowerShell

  1. #1


    hrh1818 Guest

    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

  2. #2


    Keith Hill [MVP] Guest

    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

  3. #3


    Keith Hill [MVP] Guest

    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

  4. #4


    hrh1818 Guest

    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

  5. #5


    klumsy@xtra.co.nz Guest

    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

  6. #6


    Keith Hill [MVP] Guest

    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

  7. #7


    Marcel J. Ortiz [MSFT] Guest

    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

Looking for hints on sorting a file with PowerShell

Similar Threads
Thread Forum
Sorting by file names
I have a set of text files, with the file name always in mmddyyyy.txt format. I want to sort the file by yyyy, mm, and dd. For example, I have...
PowerShell
File view sorting
I have files on as SD card mostly in the correct order for an LCD picture frame. When I view the card in windows; it sorts the files so they are in...
Vista file management
File and Folder Sorting...Help!
This may same real lame but I cannot find a way to sort and order files and subfolders by name, date etc. How do you do this in Vista? TIA ...
Vista General
Sorting by file name
Are there choices as to how to sort files in a folder? If so, how do I make choices? Generally, I like Windows to sort files within a folder by...
Vista file management
Vista File Explorer sorting and layout
I'm using Vista ultimate, on a project which has reasonably large file storage of about 30,000 files in some folders. Files are named using guids...
Vista General
File sorting in folders, how to NOT?
In XP, when I dropped a file, or files into a folder they would just go to the bottom of the list of files. This is handy, because you can see the...
Vista General
Something wrong with Vista networking? Some hints about enablingping and file sharing on local network
Hi, Like a number of others here, I've been struggling trying to get Vista (Home Premium in my case) networking working on my home LAN. I had...
Vista networking & sharing