Linux / UNIX Shell: Sort IP Address

Q. I’d like to sort a list of IP address stored in a text file. How do I sort by last notation or entire address under Linux or UNIX operating systems?

A.. You need to use sort command to displays the lines of its input listed in sorted order. Sorting is done based on one or more sort keys extracted from each line of input. By default, the entire input is taken as sort key. Blank space is taken used as default field separator.

Sort command to sort IP address

Here is our sample input file:

Type the following sort command:
$ sort -t . -k 3,3n -k 4,4n /path/to/file
Sample output:


  • -t . : Set field to . (dot) as our IPs separated by dot symbol
  • -n : Makes the program sort according to numerical value
  • -k opts: Sort data / fields using the given column number. For example, the option -k 2 made the program sort using the second column of data. The option -k 3,3n -k 4,4n sorts each column. First it will sort 3rd column and then 4th column.

Further readings:

  • man sort

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 11 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
11 comments… add one
  • Vaishnavi Sep 17, 2008 @ 10:20

    A very good example. Your site is amazing and I look forward to your posts all the time :)

  • Raymond Sep 17, 2008 @ 15:01

    I’ve been follow your site 6 months, faq section is always very informative. keep up your good work..

  • Brad Oct 16, 2009 @ 19:57

    You allude to sorting all columns in an IP address, but don’t specifically state how to.
    Here’s the sort command to sort all IP addresses:
    sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /path/to/file

    Also, since i’m here, i wanted then to reverse my IP addresses so i could build a reverse zone file for DNS. I found an easy way:
    IP=(${IP//./ })
    echo $Rev

    Thanks to you and your sort command for getting me started.

  • Vivek Umasuthan Nov 24, 2009 @ 21:14

    This was really useful. Thanks for the help.

  • Kacak Apr 14, 2011 @ 6:54

    Thanks for the great sharing… I know this is for unix/linux machine, but i would appreciate if you could share something similar (to sort ip addresses) on windows OS as well.


  • tc Jun 14, 2011 @ 12:52

    I have a text file of IP addresses that I’d like to sort, which might look something like this: 50 100 50 80
    ab32::ff0:c23a 123 ff::ab12 80 80 123

    I’d like to sort on the first column of IP addresses, then the third column of IP addresses. It doesn’t matter if IPv4 comes before IPv6 or vice versa.

    This sort method seems to work only for a text file containing a single column of IPv4 addresses. It’s a tough problem. This is a start for me, and I’ll have to do some trial and error and reading of “man sort.” Any tips would be appreciated.


  • joe Jan 15, 2015 @ 10:17

    You can also use the -V, –version-sort option :
    natural sort of (version) numbers within text

  • James Feb 23, 2015 @ 19:55

    Is it possible to sort by a column and then within that column? Say I had a file with a bunch of text similiar to:

    hostname1 has address
    hostname2 has address
    hostname3 has address
    hostname4 has address
    hostname5 has address

    …and so on. And I wanted to sort the file by the 4th column and then within *that* column sort the IPs numerically so that would show up above .. ect keeping the rest of the line intact.


  • dtkerns Sep 25, 2015 @ 15:32

    @ James

    awk '{print $4 ":" $0}' file | sort -t. -n -k1,1 -k2,2 -k3,3 -k4,4 | cut -t: -f2-
  • Jim Mar 9, 2016 @ 19:38

    Much obliged, I’ve been looking for this explanation for years.

  • Ray Jun 1, 2016 @ 19:21

    Love it! Great examples and very helpful

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum