Unix Sort by IP Address

[root@sunx4150 ~]# cat /etc/hosts

# Do not remove the following line, or various programs
# that require network functionality will fail.
10.1.50.101             sunx4150.xyz.com localhost.localdomain localhost
127.0.0.1               sunx4150.xyz.com localhost.localdomain localhost
10.0.51.50      T5220   T5220.xyz.com
10.0.50.36      ibmaix  ibmaix.xyz.com
10.0.50.9            bucnodc01.xyz.com
10.0.50.36      ibmaix.xyz.com ibmaix
10.1.50.106    ibmcl03.xyz.com     ibmcl03
10.1.51.83      nnl106.xyz.com
10.1.50.134     dbcimb01
10.0.2.20        bkonetapp1
10.112.2.101     spblrdc010
10.0.2.11        bpelapp1
[root@sunx4150 ~]#
[root@sunx4150 ~]#

[root@sunx4150 ~]# sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts

# Do not remove the following line, or various programs
# that require network functionality will fail.
10.0.2.11        bpelapp1
10.0.2.20        bkonetapp1
10.0.50.9         bucnodc01.xyz.com
10.0.50.36      ibmaix  ibmaix.xyz.com
10.0.50.36      ibmaix.xyz.com ibmaix
10.0.51.50      T5220   T5220.xyz.com
10.1.50.101             sunx4150.xyz.com localhost.localdomain localhost
10.1.50.106    ibmcl03.xyz.com     ibmcl03
10.1.50.134     dbcimb01
10.1.51.83      nnl106.xyz.com
10.112.2.101     spblrdc010
127.0.0.1               sunx4150.xyz.com localhost.localdomain localhost
[root@sunx4150 ~]#
[root@sunx4150 ~]#

[root@sunx4150 ~]# sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts | awk '$1!~/:/ &&

!/^$/ && !/^#/’
10.0.2.11        bpelapp1
10.0.2.20        bkonetapp1
10.0.50.9        bucnodc01.xyz.com
10.0.50.36      ibmaix  ibmaix.xyz.com
10.0.50.36      ibmaix.xyz.com ibmaix
10.0.51.50      T5220   T5220.xyz.com
10.1.50.101             sunx4150.xyz.com localhost.localdomain localhost
10.1.50.106    ibmcl03.xyz.com     ibmcl03
10.1.50.134     dbcimb01
10.1.51.83      nnl106.xyz.com
10.112.2.101     spblrdc010
127.0.0.1               sunx4150.xyz.com localhost.localdomain localhost

[root@sunx4150 ~]# sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts | awk '$1!~/:/ && !/^$/ && !/^#/ && !/localhost/'

10.0.2.11        bpelapp1
10.0.2.20        bkonetapp1
10.0.50.9        bucnodc01.xyz.com
10.0.50.36      ibmaix  ibmaix.xyz.com
10.0.50.36      ibmaix.xyz.com ibmaix
10.0.51.50      T5220   T5220.xyz.com
10.1.50.106    ibmcl03.xyz.com     ibmcl03
10.1.50.134     dbcimb01
10.1.51.83      nnl106.xyz.com
10.112.2.101     spblrdc010
[root@sunx4150 ~]#

Leave a Comment

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

CAPTCHA * Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top