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:
192.168.1.100 192.168.1.19 192.168.1.102 192.168.2.1 192.168.0.2
Type the following sort command:
$ sort -t . -k 3,3n -k 4,4n /path/to/file
Sample output:
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 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
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop











{ 6 comments… read them below or add one }
A very good example. Your site is amazing and I look forward to your posts all the time :)
I’ve been follow your site 6 months, faq section is always very informative. keep up your good work..
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.
This was really useful. Thanks for the help.
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
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!