Sorting text files (sort command)

Use the sort command to alphabetize lines in the files specified by the File parameters and write the result to standard output.

If the File parameter specifies more than one file, the sort command concatenates the files and alphabetizes them as one file.
Note: The sort command is case-sensitive and orders uppercase letters before lowercase (this behavior is dependent on the locale).
In the following examples, the contents of the file named names are:
marta
denise
joyce
endrica
melanie
and the contents of the file named states are:
texas
colorado
ohio
  • To display the sorted contents of the file named names, type the following:
    sort names
    The system displays information similar to the following:
    denise
    endrica
    joyce
    marta
    melanie
  • To display the sorted contents of the names and states files, type the following:
    sort names states
    The system displays information similar to the following:
    colorado
    denise
    endrica
    joyce
    marta
    melanie
    ohio
    texas
  • To replace the original contents of the file named names with its sorted contents, type the following:
    sort -o names names
    This replaces the contents of the names file with the same data but in sorted order.

See the sort command in the Commands Reference, Volume 5 for the complete syntax.