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