Linux / UNIX Shell Sort IP Address

See all Bash/Shell scripting related FAQ
I want to sort a list of IP addresses stored in a text file. How do I sort by last notation or entire address under Linux or UNIX operating systems?

It would help if you used the sort command to display the lines of its input listed in sorted order. Sorting is done based on one or more sort keys extracted from each input line. By default, the entire input is taken as sort key. Blank space is taken used as the default field separator.
Advertisement

Sort command to sort IP address in Linux and Unix shell

Here is our sample input file for shell to sort IP address:

192.168.1.100
192.168.1.19
192.168.1.102
192.168.2.1
192.168.0.2

Sort IP addresses in ascending order

Run the following command:
$ sort -t . -n -k 1,1 my_ip_address.txt
Let us break down the sort -t . -n -k 1,1 my_ip_address.txt command:

  1. sort : Use this command to sort lines of text.
  2. -t . : Since IP addresses are separated by periods, you will tell the sort command to use a period/dot (.) as the field separator.
  3. -n : Tell the sort command about numerical sort.
  4. -k 1,1 : This option tells sort command field number to sort on. For instance, the -k 1,1 option sorts on the first field, which is the first octet of the IP address. You can add multiple -k options to fine tune sorting.
  5. my_ip_address.txt : Tell sort command to sort the IP addresses in the file named my_ip_address.txt by the first octet of the IP address.

Sorting IP addresses in descending order

Pass the -r (reverse) option:
$ sort -t . -n -k 1,1 -r file_ip_address_name.txt
You can sort IP addresses by the last octet as follows:
$ sort -t . -n -k 4,4 filename
Do you want to sort IP addresses by the first three octets? Try:
$ sort -t . -n -k 1,3 filename

Debugging sort command

You can annotate the part of the line used to sort, and warn about questionable usage to screen by passing the --debug to GNU/sort. For example:
$ sort --debug -t . -n -k 4,4 ip.txt
How do I sort IP addresses in Linux or UNIX shell using the sort command

Final example about sorting IP addresses

Type the following sort command to sort IP addresses:
$ sort -t . -k 3,3n -k 4,4n /path/to/file
Here is what I see:

192.168.0.2
192.168.1.19
192.168.1.100
192.168.1.102
192.168.2.1

Where,

  • -t . : Set field to “.” (dot) as our IPs separated by dot symbol.
  • -n : Makes the program sort according to the numerical value.
  • -k opts: Sort data or 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.

How to store the sorted IP address output in a new file?

Use the >filename operator as follows. For example:
$ sort options input > output
$ sort -t . -n -k 4,4 ip.txt > sorted.ip.txt

You can now view file using the more command/bat command/less command/cat command:
$ cat sorted.ip.txt
If necessary, you can use the commands “grep” or “egrep” to search for data. This is particularly helpful when searching for information within your sorted IP address file for webserver or MySQL server IP address. For example:
$ grep ip_here sorted.ip.txt
$ grep '192.168.0.2' sorted.ip.txt

Using version sort

You can do the natural sort of (version) numbers within text. The syntax is
$ input | sort -V
$ cat file | sort -v

Let us run the following commad:
$ dig yahoo.com +short
Outputs:

74.6.231.21
74.6.231.20
74.6.143.25
98.137.11.163
74.6.143.26
98.137.11.164

Now sort it:
$ dig yahoo.com +short | sort -V
Outputs:

74.6.143.25
74.6.143.26
74.6.231.20
74.6.231.21
98.137.11.163
98.137.11.164

Summing up

I hope this page helped you sort IP addresses in Linux or UNIX shell with the help of sort command. The syntax for BSD/sort or GNU/sort is similar, but some command options may be missing. Hence, read the manual pages as explained below.

Further readings

See the following manual pages using the sort command or help command. For instance:
$ man sort
$ sort --help

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

14 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=10.120.19.18
    IP=(${IP//./ })
    Rev=${IP[3]}.${IP[2]}.${IP[1]}.${IP[0]}
    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.

    Thanks

  • 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:

    10.20.30.40 50 60.70.80.90 100
    1.2.3.4 50 5.6.7.8 80
    ab32::ff0:c23a 123 ff::ab12 80
    192.168.1.1 80 123.45.67.89 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.

    Thanks!

  • 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 192.168.2.1
    hostname2 has address 192.168.2.23
    hostname3 has address 10.5.5.5
    hostname4 has address 192.168.3.4
    hostname5 has address 192.168.2.229
    

    …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 192.68.2.23 would show up above 192.168.2.229 .. ect keeping the rest of the line intact.

    Thanks!

  • 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.