The sort
command sorts the content of stdin and passed the output to stdout.
The call signature is:
sort {options} {paths}
If one or more paths are specified, then the content from all specified files are concatenated then passed the stdin. For example, given the content in colors.csv:
·
·
ninja$:·cat·colors.csv
red,#f00
green,#0f0
blue,#00f
yellow,#ff0
cyan,#0ff
magenta,#f0f
ninja$:··
The lines of this file can be sorted using the following command:
·
·
ninja$:·sort·colors.csv
blue,#00f
cyan,#0ff
green,#0f0
magenta,#f0f
red,#f00
yellow,#ff0
ninja$:··
Several options are available to define how sorting should be executed:
Option | Description |
---|---|
-b | Ignore leading blanks |
-d | Sort based only on alphanumeric and blank characters |
-f | Ignore case |
-g | Sort based on numerical value |
-i | Ignore non-printing characters |
-M | Sort months in order |
-h | Sort according to human-readable numbers |
-n | Sort according to string-numerical value |
-R | Sort in random order, grouping identical keys |
-r | Sort in reverse |
Delimited Text
The sort
command is also capable of sorting delimited text according to one or
more fields:
Option | Description |
---|---|
-t | Define the field separator |
-k | Define the sort keys |
For example, to sort the lines in colors.csv in reverse order, according to the RGB values, first define the field separator as "," and the sort key as the second field, then reverse-sort:
·
·
ninja$:·sort·-t·,·-k·2·-r·colors.csv
yellow,#ff0
magenta,#f0f
red,#f00
cyan,#0ff
green,#0f0
blue,#00f
ninja$:··