[comp.unix.questions] sort first two fields in numeric order

mark@drd.com (Mark Lawrence) (06/19/91)

I've got a file with tab separated columns of stuff that I
expand to pretty tabstops and sort.  I want to sort the file 
primarily on the first column and then on the second, both
numerically.  No problem, right?

expand -5 foo | sort +0n -1n 

should treat both the first and second columns as numerics
and sort them in that order.  It don't.  I get:

16  0
16  1
16  10
16  19
16  2
16  21

I'm missing something terribly obvious here.  Comments?
-- 
mark@drd.com
mark@jnoc.go.jp  $B!J%^!<%/!&%i%l%s%9!K(B  Nihil novum sub solem

felps@convex.com (Robert Felps) (06/19/91)

In <1991Jun18.222520.7816@drd.com> mark@drd.com (Mark Lawrence) writes:

>I've got a file with tab separated columns of stuff that I
>expand to pretty tabstops and sort.  I want to sort the file 
>primarily on the first column and then on the second, both
>numerically.  No problem, right?

>expand -5 foo | sort +0n -1n 

>should treat both the first and second columns as numerics
>and sort them in that order.  It don't.  I get:

>16  0
>16  1
>16  10
>16  19
>16  2
>16  21

>I'm missing something terribly obvious here.  Comments?

Well, obvious, probably not terribly though. Try,

   expand -5 foo | sort +0n +1n

or

   expand -5 foo | sort +0n -1n +1n -2n

From the sort man page:
     The notation +pos1 -pos2 restricts a sort key to a field
     beginning at pos1 and ending just before pos2.
                           ^^^^^^^^^^^^^^^^^^^^^^^
The +Col +Col format, as in example 1, doesn't provide an ending
column because it is not necessary. As the SYNOPSIS line shows
if pos1 is specified pos2 is optional.

     sort [ -mubdfinrctx ] [ +pos1  [ -pos2 ] ] ...  [ -o name ]
     [ -T directory ] [ name ] ...


>-- 
>mark@drd.com
>mark@jnoc.go.jp  $B!J%^!<%/!&%i%l%s%9!K(B  Nihil novum sub solem

Hope this helps,
Robert Felps            I do not speak for  felps@convex.com
Convex Computer Corp    Convex and I seldom Product Specialist
3000 Waterview Parkway  speak for myself.   Tech. Assistant Ctr
Richardson, Tx.  75080  VMS? What's that?   1(800) 952-0379

dmturne@PacBell.COM (Dave Turner) (06/20/91)

In article <felps.677332177@convex.convex.com> felps@convex.com (Robert Felps) writes:
.In <1991Jun18.222520.7816@drd.com> mark@drd.com (Mark Lawrence) writes:
.
.>I've got a file with tab separated columns of stuff that I
.>expand to pretty tabstops and sort.  I want to sort the file 
.>primarily on the first column and then on the second, both
.>numerically.  No problem, right?
.
.>expand -5 foo | sort +0n -1n 
.
.>should treat both the first and second columns as numerics
.>and sort them in that order.  It don't.  I get:
.

Don't expand the tabstops to spaces before you sort.

On SVR[23] the following works.

	sort -n -t"	" +0 -1 +1 -2 foo

Note that there is a real tabcharacter between the ""s in the -t option. 
The ""s are needed to tell sh that the TAB is not whitespace on the command line
and that it should be passed to sort as an argument.


-- 
Dave Turner	415/823-2001	{att,bellcore,sun,ames,decwrl}!pacbell!dmturne