1

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

IP, MAC, Name, Comment 10.10.122.1, 66-55-44-33-22-11, testuser1, this is testuser 1 10.10.122.100, 66-55-44-33-22-12, testuser2, this is testuser 2 10.10.122.2, 66-55-44-33-22-13, testuser3, this is testuser 3 10.10.122.4, 66-55-44-33-22-14, testuser4, this is testuser 4 10.10.122.203, 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:

10.10.122.1 10.10.122.2 10.10.122.4 10.10.122.100 10.10.122.203 

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.

2
  • 1
    Why not sort your first csv file? It already has all the information you need? Commented Oct 27, 2014 at 7:55
  • Thanks for the tip. Unfortunately it doesn't work well. I followed the steps in the tutorial, but somehow the output looks a bit disordered. Commented Oct 27, 2014 at 9:59

1 Answer 1

3

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.

3
  • 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? Commented Oct 27, 2014 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 Commented Oct 27, 2014 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 ! Commented Oct 27, 2014 at 11:43

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.