I got two *.csv files. The first contains this

IP, MAC, Name, Comment, 66-55-44-33-22-11, testuser1, this is testuser 1, 66-55-44-33-22-12, testuser2, this is testuser 2, 66-55-44-33-22-13, testuser3, this is testuser 3, 66-55-44-33-22-14, testuser4, this is testuser 4, 66-55-44-33-22-15, testuser5, this is testuser 5

The second one contains the sorted list of the IPs. This was a little bit tricky 'cause you had to add leading zeros for sorting the IPs and then you need to remove the leading zeros. So the file looks like this:

How can I get the other attributes e.g. like the suitable MACS for the IPs in the second csv file?

I tried to use where-object but unfortunately I don't know how to compare the IPs and then to link the other attributes for the correct IPs.


Just sort the original csv:

$data = Import-Csv first.csv
$data = $data | Sort-Object {"{0:000}.{1:000}.{2:000}.{3:000}" -f @([int[]]$_.IP.split("."))}

Now, since we've treated each octet of the IP as a zero-padded string during sorting, they will be listed in the right order.

  • Unfortunately they don't. I got a large list of addresses and the output looks disordered. I sorted the addresses with this $data | %{"{0:000}.{1:000}.{2:000}.{3:000}" -f @([int[]]$_.IP.split('.'))} | sort | %{"{0}.{1}.{2}.{3}" -f @([int[]]$_.split('.'))} Linkink the attributes would be easier to understand for me. Can't I overload "sort-object" to extend it with the sorting code? – Sascha R. Oct 27 '14 at 10:14
  • 1
    Oh, my bad, you're absolutely right. Just extend the sorting block to use $_.IP in place of $_ and you should be good to go, check the update – Mathias R. Jessen Oct 27 '14 at 10:52
  • Oh my god. I just can't believe it. I searched hours and hours how to do that properly. I nearl wanted to overload sort-object for that. Thanks dude. It worked perfectly ! – Sascha R. Oct 27 '14 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.