Sorting in file. Pls help me

This topic contains 9 replies, has 5 voices, and was last updated by Profile photo of Anthony Stringer Anthony Stringer 6 months, 1 week ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #36498
    Profile photo of Oleg Ieroshenko
    Oleg Ieroshenko
    Participant

    Hello. I have next content of file :
    10.0.17.0 | do not use | reserved for future rtr/fw/gwy |
    10.0.17.1 | core-sw | vlan 500 (mgmt) |
    20.0.17.255 | do not use | reserved for future rtr/fw/gwy) |
    10.0.17.5 |Pod17-Edge-FW| vlan 500 (mgmt) |
    How can I sort lines of file by IP-addresses. Thanks in advance

    #36504
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    You can use sort-object: This is what I got:

    Before Script
    PS C:\WINDOWS\system32> gc C:\Test\iptxt.txt
    10.0.17.0 | do not use | reserved for future rtr/fw/gwy |
    10.0.17.1 | core-sw | vlan 500 (mgmt) |
    20.0.17.255 | do not use | reserved for future rtr/fw/gwy) |
    10.0.17.5 | Pod17-Edge-FW| vlan 500 (mgmt) |

    After using sort-object:
    PS C:\WINDOWS\system32> gc C:\Test\iptxt.txt | Sort-Object
    10.0.17.0 | do not use | reserved for future rtr/fw/gwy |
    10.0.17.1 | core-sw | vlan 500 (mgmt) |
    10.0.17.5 | Pod17-Edge-FW| vlan 500 (mgmt) |
    20.0.17.255 | do not use | reserved for future rtr/fw/gwy) |

    #36517
    Profile photo of random commandline
    random commandline
    Participant
    # If the pipe, '|', exists in file, create pscustomobject
    Get-Content .\myfile.txt | ConvertFrom-Csv -Delimiter '|' -Header 'IP Address','Device','VLAN' | Sort-Object -Property 'IP Address'
    
    #36521
    Profile photo of Oleg Ieroshenko
    Oleg Ieroshenko
    Participant

    Thanks Wilfredo. I tried to do it via regular expression, but it is easy that I thought. 🙂

    #36648
    Profile photo of Oleg Ieroshenko
    Oleg Ieroshenko
    Participant

    it doesn't work correct 🙁 P
    Get-Content $MDPATH|Sort
    | 10.0.16.11 |Pod16-AD| vlan 500 (mgmt) |
    | 10.0.16.255 | do not use | reserved for future rtr/fw/gwy) |
    | 10.0.16.5 |Pod16-Edge-FW| vlan 500 (mgmt) |
    | 10.0.16.50 |Pod16-smc| vlan 500 (mgmt) |
    | 10.0.16.51 |Pod16-flow-col| vlan 500 (mgmt) |
    | 10.0.16.52 |Pod16-flow-repl| vlan 500 (mgmt) |
    | 10.0.16.53 |Pod16-flow-sensor| vlan 500 (mgmt) |
    | 10.0.16.55 |Pod16-ISE| vlan 500 (mgmt) |

    #36650
    Profile photo of random commandline
    random commandline
    Participant

    If the pipe, '|', is used to separate parts of your file, then my previous post should work.

    #36652
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    No it won't. That's because you're sorting as a string. In that case "10.0.16.255" comes before "10.0.16.5". You need to convert the IP address into something that is sortable. The trick most often used is to cast it at [version] which can then be properly sorted. For instance ...

    $data = Get-Content -Path C:\Ephemeral\iptext.txt | 
        ConvertFrom-Csv -Delimiter '|' -Header 'IP','Name','Notes','Other'
    $results = foreach ($item in $data)
    {
        [PSCustomObject]@{
            IP = [version]$item.IP
            Name = $item.Name
            Notes = $item.Notes
            Other = $item.Other
        }
    }
    $results | sort -Property IP | Out-GridView
    

    This also assumes correct header alignment, which I can't guarantee since your two samples are different, e.g., your second sample has a leading bar character whereas your first didn't.

    #36654
    Profile photo of random commandline
    random commandline
    Participant

    Bob, you are right. After testing with a larger list, I noticed 10.0.16.255 was listed before 10.0.16.5. My post worked with the initial (smaller) list, but not a larger list. Casting the IP address is required.

    # Cast
    select-object -Property @{n='ip address';exp={[version]$_.'ip address'}}
    
    #36658
    Profile photo of Oleg Ieroshenko
    Oleg Ieroshenko
    Participant

    yes. it works. Thanks you. How i can add again my delimeter '|' to file?

    #36662
    Profile photo of Anthony Stringer
    Anthony Stringer
    Participant
    $results = Get-Content C:\Test\iptxt.txt |
        ConvertFrom-Csv -Delimiter '|' -Header 'IP','Name','Notes','Other' |
        Sort-Object {[version]$_.ip}
    
    $results | Export-Csv C:\Test\iptxtresults.csv -Delimiter '|' -NoTypeInformation
    
    # another (not recommended) option so the final result does not have quotes.
    # it appears that your original file has a space on either side of the '|'
    # you can add that back in the following line if needed
    $results | % {"$($_.ip)|$($_.name)|$($_.notes)|$($_.other)"} | Out-File C:\Test\iptxtresults.txt
    
Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.