Sorting Files or Pipelines

Sorting means to arrange lines in alphabetical order. To sort one or more files, use the sort command, as in the following:

sort file1 file2 file3

sort will merge all the specified files into its workspace, sort the workspace, and then display the result. The sort command offers many features to enable you to control what characters or fields are used to order the lines.

Why Don’t UNIX Commands Have Built-in Sort or Paging Options?

If a user wants sorted output, or paged output, or sorted output one page at a time from any UNIX command, it is expected that the user will just pipe to sort, or pipe to pg, or pipe to sort and then to pg. In this way, UNIX utilities don’t duplicate efforts and can be more efficient.

Get Practical UNIX 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.