Search code examples
linuxbashsortingip

Sorting a file with IP


i have a file with the following lines:

ex.

host1   169.254.228.92
host2   169.254.230.182
host3   169.254.163.79

and i want to sort it ascending by 3 column first and then 4

when im using :

sort -n -t .  -k 3,3  -k 4,4 test.txt 

it does not work properly, it only sorts by 3 column :

host13  169.254.10.154
host12  169.254.18.77
host14  169.254.74.233

Any solution ?


Solution

  • Try this:

    sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 test.txt 
    

    Sort first by the first field, and only the first field (-k 1,1), then by the second and only the second (-k 2,2), and so on (-k 3,3 -k 4,4).

    Or just use sort -V.