metrics.csv stores the numeric value in field 3.
Sort rows by that field from largest to smallest.
api,east,9
web,west,120
db,east,45Which command is correct?
The comma delimiter, key range 3,3, numeric comparison, and reverse order are expressed by -t, -k3,3nr.
Detailed explanation
sort -t, -k3,3 metrics.csvIncorrect. It compares field 3 lexically and in ascending order.
Incorrect. It compares field 3 lexically and in ascending order.
sort -t, -k3,3n metrics.csvIncorrect. Numeric comparison is enabled, but the order is ascending.
Incorrect. Numeric comparison is enabled, but the order is ascending.
sort -t, -k3,3nr metrics.csvCorrect. It selects field 3 and uses numeric reverse order.
Correct. It selects field 3 and uses numeric reverse order.
sort -t, -k2,2nr metrics.csvIncorrect. It sorts by field 2 instead of the numeric field.
Incorrect. It sorts by field 2 instead of the numeric field.
Try it yourself
An example you can run in a temporary verification environment.
printf '%s
' 'api,east,9' 'web,west,120' 'db,east,45' > metrics.csv
LC_ALL=C sort -t, -k3,3nr metrics.csvExpected result
web,west,120
db,east,45
api,east,9Key points
- Field delimiters in sort
- Numeric versus lexical comparison
- Reverse sorting
Notes
- Environment: GNU coreutils 9.x / LC_ALL=C
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.