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.

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
  • Ray Jun 1, 2016 @ 19:21

    Love it! Great examples and very helpful

  • Cregga Aug 4, 2023 @ 4:24

    Dear M. Gite,
    Never too late to say thank you for a wickedly helpful how-t!
    Thanks

  • vk Nov 20, 2023 @ 16:43
    :~$ dig gmail.com +short | sort --version-sort 
    64.233.164.17
    64.233.164.18
    64.233.164.19
    64.233.164.83

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.