February 2, 2016

Unix/Linux: Sort command to sort IP Address

Sometime while dealing with list of  IP Address, we might need to sort IP Address in order. "sort" command can be use for sorting such list. But one must know how to properly use sort command to sort IP Addresses because of its dotted-quad notations.

While using sort command without options, it sorts list of IP Address based upon alphabetic order.

Though using sort -n  will sort list of IP Address in numerical order but still it has limitation because of dotted-quad notation of IP Address. Therefore the correct way to sort IP Address is to order the list numerically by considering IP Address as set of four fields having numeric value separated by dot.

Lets take a example:

$ cat ip.txt
9.1.4.4
9.1.4.4
9.1.78.4
149.4.78.4
149.4.78.41
14.4.78.41
10.4.7.41



$ sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 ip.txt
9.1.4.4
9.1.4.4
9.1.78.4
10.4.7.41
14.4.78.41
149.4.78.4
149.4.78.41


where,
  • -t : Set field to . (dot)
  • -n : sort list numerically
  • -k options:  Sort via a key using start and stop position




5 comments:

  1. Thank you so much as you have been willing to share information with us. We will forever admire all you have done here because you have made my work as easy as ABC. https://192-168-i-i.com/

    ReplyDelete
  2. Much thanks for keep this data. check my ip address

    ReplyDelete
  3. Pretty good post. I have just stumbled upon your blog and enjoyed reading your blog posts very much. I am looking for new posts to get more precious info. Big thanks for the useful info. https://192-168-i-i.com/

    ReplyDelete
  4. There is a norm of correspondence which is called an Internet Protocol standard (IP). A straightforward regular similarity would be your road address. With the goal for you to get snail mail at home the sending party must have your right postage information (IP address) in your city (system) or you don't get your mail.192.168.l0.1

    ReplyDelete
  5. Since we are more acquainted with the Linux working framework, we can take note of the numerous manners by which Linux contrasts from the universes most well known OS, Microsoft Windows. https://onohosting.com/

    ReplyDelete