Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I have got a dataset like this:

plym fury 1970 73 2500 
chevy malibu 1999 60 3000
ford mustang 1965 45 10000
volvo s80 1998 102 9850
ford thundbd 2003 15 10500
chevy malibu 2000 50 3500
bmw 315i 1985 115 450
honda accord 2001 30 6000
ford taurus 2004 10 17000
toyota rav4 2002 180 750
chevy impala 1985 85 1550
ford explor 2003 25 9500

I am supposed to sort the file by the manufacturer and then by the price within a manufacturer.

The book says that this command will do it:

sort -t ' '  a -k1,1 -k5

My question is that what is the difference between the command above and this command

sort -t ' '  a -k1 -k5

I checked the output of both of command alone with -k1 and -k1,1 like this

sort -t ' '  a -k1,1 

sort -t ' '  a -k1

and both of them were the same, and why is it that when are adding -k5 the output in these two cases are different.

I am not able to understand the difference between -k1 and -k1,1 properly in terms of functioning,

Can anyone explain this to me with the context of the given dataset.

share|improve this question

marked as duplicate by Gilles, jasonwryan, rahmu, uther, Michael Mrozek Feb 7 '13 at 3:28

This question was marked as an exact duplicate of an existing question.

Have you read the man page? BTW, that should be -k5n instead of -k5 – Stéphane Chazelas Feb 6 '13 at 22:37
up vote 0 down vote accepted

The man page says:

   -k, --key=POS1[,POS2]
          start  a  key at POS1 (origin 1), end it at POS2 (default end of
          line).  See POS syntax below

This means if you do not specify you implicitly specify all following columns. If you specify multiple columns, sort will only sort by a column if they are equal in the column before.

See this example:

$ cat test
1 3 1
1 2 3
1 1 2
$ sort test -k 1 -k3
1 1 2
1 2 3
1 3 1
$ sort test -k 1,1 -k3
1 3 1
1 1 2
1 2 3

The first sort says: First sort on column 1,2,and 3, and if they are the same sort on column 3. Obviously sorting on 1,2,3 is already enough for a final order.

The second sort says: First sort on column 1 to 1 (i.e. 1 only) and in case the order is still not clear sort on column 3. Now sort is not able to find an order by looking at column 1 and it will sort by column 3, too.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.