Basics
- Reverse:
sort -r - Random:
sort -R
Sorting by field
We specify field by the -k option.
sort -k2 <file>
but this will do a lex order sort on that field. If we want a numerical sort:
sort -n k2 <file> # or
sort -g k2 <file>
the first, sort -n sorts integers, the second sorts floats and 1.3e4 too.
Consider the input
hello 1 flibble
hello 2 flibble
hello 10 flibble
hello 13 flibble
hello 21 flibble
hello 21.1 flibble
hello 21.2 flibble
hello 22 flibble
hello 2.21e1 flibble
hello 23 flibble
then sort -k2 will output
hello 1 flibble
hello 10 flibble
hello 13 flibble
hello 2 flibble
hello 2.21e1 flibble
hello 21 flibble
hello 21.1 flibble
hello 21.2 flibble
hello 22 flibble
hello 23 flibble
and sort -k2 -n will output (note how 2.21e1 lies between 2 and 10)
hello 1 flibble
hello 2 flibble
hello 2.21e1 flibble
hello 10 flibble
hello 13 flibble
hello 21 flibble
hello 21.1 flibble
hello 21.2 flibble
hello 22 flibble
hello 23 flibble
and sort -k2 -g will output (now 2.21e1 == 22.11 is in the correct place)
hello 1 flibble
hello 2 flibble
hello 10 flibble
hello 13 flibble
hello 21 flibble
hello 21.1 flibble
hello 21.2 flibble
hello 22 flibble
hello 2.21e1 flibble
hello 23 flibble