locked
Sort text file contents by date RRS feed

  • Question

  • Hi 

    I have a text file that contains dates and data  I need to sort the contents of the text file so with the most recent date being listed 1st / ordered from most recent down

    I would use get-content  to import the content of the text file into  a variable but I'm sure of what approach/cmdlets to use next

    e.g.   Below   

    Example.txt

    1/1/2016 16:00 Server1

    1/1/2014 17:00 Server3

    2/2/2019 18:00 Server5

    2/2/2015 19:00 Server4



    confuseis

    Thursday, April 4, 2019 7:49 PM

All replies

  • Please see the following.

    help sort -online


    \_(ツ)_/

    Thursday, April 4, 2019 8:33 PM
  • Hi,

    Thanks for your question.

    You need to convert the date to DateTime object, then you can sort them by date time. Also, the CSV file is more convenient to complete your needs.

    For example:

    $test = Import-Csv -Header @("date","time","server") -Path "C:\temp\test.txt" -Delimiter " "
    $oz = new-object Globalization.CultureInfo("en-US")
    $test | Sort-Object {[System.DateTime]::Parse($_.date,$OZ)}

    You can export the sort output to a new file by "Export-CSV" cmdlet.

    Best regards,

    Lee



    Just do it.

    Friday, April 5, 2019 3:20 AM
  • Hi,

    Thanks for your question.

    You need to convert the date to DateTime object, then you can sort them by date time. Also, the CSV file is more convenient to complete your needs.

    For example:

    $test = Import-Csv -Header @("date","time","server") -Path "C:\temp\test.txt" -Delimiter " "
    $oz = new-object Globalization.CultureInfo("en-US")
    $test | Sort-Object {[System.DateTime]::Parse($_.date,$OZ)}

    You can export the sort output to a new file by "Export-CSV" cmdlet.

    Best regards,

    Lee



    Just do it.

    Won't work.


    \_(ツ)_/

    Friday, April 5, 2019 3:31 AM
  • Import-Csv C:\temp\test.txt -Header date,time,server -Path  -Delimiter ' ' |
        Select *, @{n='DateTime';e={[datetime]($_.date + ' ' + $_.time)}} |
        Sort-Object DateTime -Descending

    I think this is what you are trying to do:

    An easier method is:

    Get-Content C:\temp\test.txt |
        ForEach-Object{
            $parts = $_ -split '\s+'
            $dtstring = $parts[0,1] -join ' '
            [pscustomobject]@{
    Date = $parts[0]
    Time = $parts[1] Server = $parts[2] DateTime = [datetime]$dtstring } } | Sort-Object DateTime -Descending


    \_(ツ)_/



    Friday, April 5, 2019 3:39 AM
  • You're right. My code only can sort it by the date, but when created the date on the same day, it won't work. Your script is very effective.

    Just do it.

    Friday, April 5, 2019 6:04 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Monday, April 8, 2019 8:51 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Wednesday, April 10, 2019 9:07 AM