How does -k flag work in sort command?
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.
Related videos on Youtube
dwwdw
Updated on September 18, 2022Comments
-
dwwdw over 1 year
I have got a dataset like this:
manufacturer,model,year,mileage,price 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 thissort -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.
-
Stéphane Chazelas about 11 yearsHave you read the man page? BTW, that should be
-k5n
instead of-k5
-