8.2 File Processing Commands
8.2.4 Sorting output
The sort command provides a quick and easy way for operators to organize data in either numerical or alphabetical order. This command uses the ASCII character set as its sorting order, working from left to right on a character-by-character basis. By default, sort relies on white space to delimit the various fields within the data of a file. Some sort features include:
  • Multilevel sorting
  • Field-specific sorting
  • Accepting standard input
  • Producing standard output
Command Format:
sort [options] [input_filename]

Figure shows the various options available with the sort command. These enable the operator to define the type of sort to perform as well as the field on which to begin sorting. Figure shows some basic examples using the sort command with a simple format. The cat command displays the unsorted contents of fileA. The first sort command produces an ASCII type of sort, beginning with the first character of each line. The second sort example is a numerical sort on the second field (sort skips one separator with the +1 syntax).

Figure shows some more advanced options sorting on different fields within a file. The first example represents beginning a sort on some field other than the first, and shows a numeric, reverse-order example. The sort command line would read as, "Do a reverse order, numeric sort on the fifth field of the data in the file list, and place the output into a file called num.list."

The second example represents a multilevel sort showing how the M option would work. This command would read as, "Do a Month order sort beginning with the sixth field. Do a second-level sort in numeric order and begin on the seventh field (this will sort the numeric day of the month correctly), name the output file update.list."
Interactive Media Activity (Flash, 105 kB)
  Using the sort Command

In this media activity, you are logged in as user2 and your current working directory is /home/user2.
You are working with a file named projects in your current directory. The contents of the file are: Project Name, Project Manager, and the Estimated Year of Completion.

Lab Activity (Adobe Acrobat Reader, 54 kB)
  File Processing Commands

In this lab, you will use advanced UNIX commands to find files and specific strings contained in files. CDE File manager will be used to locate files based on file name or file contents.