DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Working with files and directories

Sorting the contents of a file

You can sort a file containing lines of text or numerical data in a variety of ways using the sort(C) command. For example, suppose you have a file called names containing the following:

   perry
   john
   sarah
   charles
To sort its contents alphabetically, enter the following command:
   $ sort names
   charles
   john
   perry
   sarah
To direct the sorted output to a file (names1) rather than the screen (standard output), you can use either of the following command lines:
   $ sort -o names1 names
   $ sort names > names1
You can cause the original file to be sorted by giving the original filename for both arguments.

You can make sort merge two files together, in order. To do this, type the following:

sort filename1 filename2 > filename3

This creates filename3, which contains the sorted, merged contents of filename1 and filename2. (The sort command sorts the files as it merges them.) You can use the -u option to tell sort to make sure that each line in filename3 is unique; that is, if both filename1 and filename2 contain an identical line, only one copy of the line will be written to filename3:

   $ cat file1
   perry
   john
   sarah
   charles
   $ cat file2
   susanna
   charles
   bridget
   john
Running the sort command on these files merges the contents and places them in alphabetic order, as follows:
   $ sort -u file1 file2 >file3
   $ cat file3
   bridget
   charles
   john
   perry
   sarah
   susanna
There are several more options that can be used with sort. For example, -r sorts in reverse order; -n sorts on numerical order, not text order; -M causes sort to assume that the first three characters of the field being sorted are months (like ``JAN'', ``FEB'', ``MAR'', and so on) and sorts them into date order.

You can make sort select any field in a line and have it base its comparisons on that field, as follows:

   $ cat birthdays
   charles FEB
   bridget DEC
   sarah   JAN
   $ sort -M +1 birthdays
   sarah   JAN
   charles FEB
   bridget DEC
The +1 flag tells sort to make comparisons between records on the basis of the second field of each line. So, the month abbreviation on each line of the file is used as the basis for the sort operation above, and not the alphabetic order of the first field.

If you have a file where data records are made up of fields separated by some special character (called a ``separator''), you can tell sort to use that separator by using the -t option, as follows:

   $ cat birthdays
   charles:FEB
   bridget:DEC
   sarah:JAN
   $ sort -M +1 -t: birthdays
   sarah:JAN
   charles:FEB
   bridget:DEC


Next topic: Searching for text in a file
Previous topic: Comparing files

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003