Search code examples
sortingvimip-address

Sorting IP addresses in vim


I have just discovered the command :sort n in vim (how did I not know about that?!), which has almost done exactly what I need.

What I am trying to sort, though, is a long list of IP addresses (it's an "allow hosts" file to be Included into our apache config), and it would be nice for :sort n to be able to recognise that 123.45.6.7 should sort before 123.45.16.7 (for example).

Is it a safe assumption that I should be less OCD about it and not worry, because I'm not going to be able to do this without a mildly-complex sed or awk command or something?

To be clear, the rows all look something like:

Allow from 1.2.3.4
Allow from 5.6.7.8
Allow from 9.10.11.12

etc


Solution

  • Vim sort seems to be stable in practice (but it is not guaranteed). Therefore you can try:

    :%sort n /.*\./
    :%sort n /\.\d\+\./
    :%sort n /\./
    :%sort n
    

    Which will sort by number after the last dot (* is greedy), then by number after the first dot following a dot and digits, then by number after the first dot, and last by the first number.