sort
doesn’t seem to like my key specification. Why?
~/tmp $ sort --version sort (GNU coreutils) 8.25 Packaged by Cygwin (8.25-1) ~/tmp $ echo 'a;b;c;d;e;f;g'|sort --field-separator=';' --key=1,5,2 sort: stray character in field spec: invalid field specification '1,5,2'
From the man page:
-k, –key=KEYDEF : sort via a key; KEYDEF gives location and type
KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number and C a character position in the field; both are origin 1, and the stop position defaults to the line’s end.
Since the .C and OPTS part in the KEYDEF is optional, a key specification F,F,F (i.e. just the field numbers) should be correct. What did I do wrong?
BTW, my environment is Cygwin, running the Z-shell.
Advertisement
Answer
The two fields in -k arg are the START AND END fields. You can specify -k ANY NUMBER OF TIMES, to sort on multiple keys. So, -k 1,1 -k 2,2 -k 3,3 will sort first on field 1, then field 2 then field 3.