Reasoning for the syntax of the "sort" key (-k)

When using the sort function in the shell, the preferred syntax for the -k option is when sorting with only one field, for example, -k5n,5 . What is the advantage of ,5 in this case? -k5n works the same way, or at least seems to me.

Refs:

+7
linux bash shell
source share
2 answers

Suppose your data has fields N -k5n equivalent to -k5,Nn , which means that the data will be sorted using fields from 5 to N as the key. This may be undesirable, for example, if you want to have a stable look that does not change the relative order of the input entries with equal values ​​for the 5th field. Using -k5,5n makes it explicit that you want to sort only the 5th field.

+2
source share

The second key indicates the stop position, which by default corresponds to the end of the line.

That way, you could omit this, and often it didn't have practical differences, but it could be significant if, for example, you used the -u option only to output lines with unique key matches.

In addition, you can specify several key parts with several -k options, so in this case you need to specify the end point of the key part.

There are probably other cases where this matters.

0
source share

All Articles