[conspire] sort -t.

Rick Moen rick at linuxmafia.com
Tue Nov 7 12:57:08 PST 2006


Quoting 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
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^

Huh!  I did figure out that I needed "-t." (or, equivalently,
"--field-separator=.") -- but I can't find your field-specification strings
described in the manpage or texinfo docs.  When I dug into the latter,
what I found instead was a suggestion to use the "-k" (key) option.
Looking more closely, I now find this reference in the info docs:

   On older systems, `sort' supports an obsolete origin-zero syntax
   `+POS1 [-POS2]' for specifying sort keys.  POSIX 1003.1-2001 (*note
   Standards conformance::) does not allow this; use `-k' instead.

Your Unix-greybeard credentials are showing, Tom.  ;->


My solution, using "-k", _did_ end up being uglier than yours by a fair
measure.  The info docs say:

`-k POS1[,POS2]'
`--key=POS1[,POS2]'
     Specify a sort field that consists of the part of the line between
     POS1 and POS2 (or the end of the line, if POS2 is omitted),
     _inclusive_.  Fields and character positions are numbered starting
     with 1.  So to sort on the second field, you'd use `--key=2,2'
     (`-k 2,2').  See below for more examples.


I thus up with:

$ sort -u -n -t. -k 1,1 -k 2,2 -k 3,3 -k 4,4 ip > ip.sorted

The "-u" is for uniq-ing on the fly.  "-n" is a numeric-value sort
appropriate for most types of numbers (that don't use leading plus
characters or exponential notation, blessedly unlikely in IP addresses).
"-t." specifies that period is the applicable field separator (rather
than whitespace).  

Which leaves the "-k" string (equivalent to your origin-zero specifiers): 
It says, "Hey, stupid sort program!  Now that I've handed you a clue
about where the fields begin and end, _and_ told you to sort by numeric
value, please also be aware that I'd like you to find a number, then a
second number, then a third number, then a fourth number.  Kindly use
all four _as numbers_ when you sort this puppy."

What a hassle.  First, you have to say "Use numbers", then you have to
add "...and I mean, specifically, four of them."






More information about the conspire mailing list