Author Posts

March 14, 2016 at 6:15 am

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

March 14, 2016 at 7:13 am

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) |

March 14, 2016 at 8:54 am

# 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'

March 14, 2016 at 9:20 am

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

March 17, 2016 at 4:29 am

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) |

March 17, 2016 at 4:50 am

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

March 17, 2016 at 5:35 am

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.

March 17, 2016 at 6:26 am

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'}}

March 17, 2016 at 6:42 am

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

March 17, 2016 at 7:33 am

$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