Name
sort [options
] [files
] — coreutils
Synopsis
/bin
stdin stdout - file -- opt --help --version
The sort
command prints lines of text in alphabetical order, or sorted by some other rule you specify. All provided files are concatenated, and the result is sorted and printed.
$ cat myfile def xyz abc $ sort myfile abc def xyz
Useful options | |
| Case-insensitive sorting. |
| Sort numerically (i.e., 9 comes before 10) instead of alphabetically (10 comes before 9 because it begins with a “1”). |
| Another numerical sorting method with a different algorithm that, among other things, recognizes scientific notation (7.4e3 means “7.4 times ten to the third power,” or 7400). Run |
| Unique sort: ignore duplicate lines. (If used with |
| Don’t sort, just check if the input is already sorted. If it is, print nothing, otherwise print an error message. |
| Ignore leading blanks. |
| Reverse the output: sort from greatest to least. |
| Use |
| Choose sorting keys. |
A sorting key is a portion of a line that’s considered when sorting, instead of considering the entire line. An example is “the fifth character of each line.” Normally, sort
would consider these lines to be in sorted order:
aaaaz bbbby
but if your sorting key is “the fifth character of each line,” then the lines are reversed because y comes before z. The syntax means:
Item | Meaning | Default if not ... |
Get Linux Pocket Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.