sort mystery

Hi everyone,

I can't seem to understand the behavior of sort on a particular case.

cat tmp25
1    a
10    b
20    c
2    d

I do that:

sort -k1,1 tmp25
1    a
10    b
2    d
20    c

This one I understand, it's what i expected, from a string point of view 1<10<2<20

sort -k1 tmp25
10    b
1    a
20    c
2    d

But this one????

Anyone understands why it gives that order? :eek:
Thanks!!

Anthony

I get same results for both the commands

$ sort -k1,1 file
1    a
10    b
2    d
20    c
$ sort -k1 file
1    a
10    b
2    d
20    c

That's odd..

It's a direct copy/paste from my terminal....:

14:33:55 anthony:~$ echo -e "1\ta\n10\tb\n20\tc\n2\td" | sort -k1,1
1    a
10    b
2    d
20    c
14:33:56 anthony:~$ echo -e "1\ta\n10\tb\n20\tc\n2\td" | sort -k1
10    b
1    a
20    c
2    d
14:33:56 anthony:~$ 

what did you expect?

sort -n -k1,1 tmp25

Because in the second sort where you say that the key is one character long, the sort program does not necessarily maintain keys that are equal on a fifo basis.

Hi vgersh99,
Thanks but I actually want a sort based on the string values. I try to understand that 2nd output as it's a mystery to me!

Jgt,
wait i'm not sure I understand. i thought -kX1,Y1 -kX2, Y2... meant that the sort were to happen on field X1 to Y1 and to X2 to Y2. Does it have to do with the position of CHARACTER, not field??

Can you modify your input data, maybe you have already saved the unsorted file to disk.
Change the tmp25 file so that the file actually is
10<tab>a
1<sp><tab>b
20<tab>c
2<sp><tab>d

Where <sp> is space and <tab> is tab.
It may be that your sort program is restricting the length of key1 to the shortest key 1.

ok I got it, i misunderstood "-k1". I thought it meant -k1[,1], that default for POS2 was same as POS1, when actually it means -k1,n where n is the last field. And since 10 < 1\\t, it all make sense.
Thanks everyone for trying to help though!

jgt is mistaken. neither of your sort commands specify a one character key. -k1,1 specifies the first field. -k1 specifies the first field through to the last character in the line.

Shouldn't command be.

sort -k 1n,1n tmp25