[conspire] Puzzle: How do you sort IP address lists?

Eric De Mund ead-conspire at ixian.com
Tue Nov 7 15:21:23 PST 2006

Rick, Tom, All,

Tom Macke <macke at scripps.edu>:
] Use -t. to break the lines into fields on ., then sort 4 ints from
] left to right:
]     sort -t. +0n -1 +1n -2 +2n -3 +3n -4 <ip.list > ip.list.sort

Using the recommended POSIX option for specifying a sort field:

    sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n <ip.list >ip.list.sort

What a pain that was! All these gyrations are due to:

    Note that if you had written `-k 2' instead of `-k 2,2' `sort' would
    have used all characters beginning in the second field and extending
    to the end of the line as the primary numeric key. For the large
    majority of applications, treating keys spanning more than one field
    as numeric will not do what you expect.

    (from the GNU sort info page)

"If there's one thing above all a vulture can't stand, it's a glass
eye." --Frank McKinney Hubbard

Eric De Mund              |      Ixian Systems      | 
email: <ead at ixian.com>    | 650 Castro St, #120-210 | ICQ: 811788
http://www.ixian.com/ead/ | Mountain View, CA 94041 | Y!M: ead0002

More information about the conspire mailing list