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
File view sorting Vista file management
File and Folder Sorting...Help! Vista General
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