z/OS UNIX System Services User's Guide
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Sorting file contents

z/OS UNIX System Services User's Guide
SA23-2279-00

When you create a file of records, you usually do not type the information in any particular order. However, you may want to keep lists in some useful order after you have entered the information. To sort the records in a file, use the sort command. sort assumes two things:
  • Your file contains one record per line. To put it another way, there is a single <newline> character between a record and the next record.
  • The fields in a record are separated by recognizable characters. In the sample file comics.lst in /samples (shown in Figure 1), we use colons.
Figure 1. A sample file: comics.lst
 Detective Comics:572:Mar:1987:$1.75
 Demon:2:Feb:1987:$1.00
 Ex-Mutants:1:Sep:1986:$2.60
 Justice League of America:259:Feb:1987:$1.00
 Boris the Bear:1:Sep:1986:$1.50
 Flaming Carrot:14:Oct:1986:$2.75
 Demon:4:Apr:1987:$1.00
 The Question:1:Jan:1987:$2.10
 Elektra:7:Feb:1987:$2.00
 Howard the Duck:29:Jan:1979:$0.35
 Wonder Woman:3:Apr:1987:$1.00
 Justice League of America:261:Apr:1987:$1.00
To sort a file such as our comic book file, enter:
sort /samples/comics.lst
This command sorts the list and displays it. To save the sorted list in a file, enter:
sort /samples/comics.lst >filename
where filename is the name of the file where you want to store the sorted list. For example:
sort /samples/comics.lst >sorted.lst
sorts the file and stores the result in sorted.lst without changing the input file.

When you use >filename to redirect sorted output into a file, you may want to make the output file name different from the (unsorted) input file name. If you want to overwrite a file with its sorted contents, see the description of the –o flag in the sort command description in z/OS UNIX System Services Command Reference.

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014