[Next] [Previous] [Up] [Top] [Contents]

8.1 Working With Files

8.1.8 sort - sort file contents

The sort command is used to order the lines of a file. Various options can be used to choose the order as well as the field on which a file is sorted. Without any options, the sort compares entire lines in the file and outputs them in ASCII order (numbers first, upper case letters, then lower case letters).

Syntax

sort [options] [+pos1 [ -pos2 ]] file

Common Options

-b ignore leading blanks (<space> & <tab>) when determining starting and ending characters for the sort key

-d dictionary order, only letters, digits, <space> and <tab> are significant

-f fold upper case to lower case

-k keydef sort on the defined keys (not available on all systems)

-i ignore non-printable characters

-n numeric sort

-o outfile output file

-r reverse the sort

-t char use char as the field separator character

-u unique; omit multiple copies of the same line (after the sort)

+pos1 [-pos2] (old style) provides functionality similar to the "-k keydef" option.

For the +/-position entries pos1 is the starting word number, beginning with 0 and pos2 is the ending word number. When -pos2 is omitted the sort field continues through the end of the line. Both pos1 and pos2 can be written in the form w.c, where w is the word number and c is the character within the word. For c 0 specifies the delimiter preceding the first character, and 1 is the first character of the word. These entries can be followed by type modifiers, e.g. n for numeric, b to skip blanks, etc.

The keydef field of the "-k" option has the syntax:

start_field [type] [ ,end_field [type] ]

where:

start_field, end_field define the keys to restrict the sort to a portion of the line

type modifies the sort, valid modifiers are given the single characters (bdfiMnr) from the similar sort options, e.g. a type b is equivalent to "-b", but applies only to the specified field

Examples

In the file users:

jdoe John Doe 4/15/96

lsmith Laura Smith 3/12/96

pchen Paul Chen 1/5/96

jhsu Jake Hsu 4/17/96

sphilip Sue Phillip 4/2/96

sort users yields the following:

jdoe John Doe 4/15/96

jhsu Jake Hsu 4/17/96

lsmith Laura Smith 3/12/96

pchen Paul Chen 1/5/96

sphilip Sue Phillip 4/2/96

If, however, a listing sorted by last name is desired, use the option to specify which field to sort on (fields are numbered starting at 0):

% sort +2 users:

pchen Paul Chen 1/5/96

jdoe John Doe 4/15/96

jhsu Jake Hsu 4/17/96

sphilip Sue Phillip 4/2/96

lsmith Laura Smith 3/12/96

To sort in reverse order:

% sort -r users:

sphilip Sue Phillip 4/2/96

pchen Paul Chen 1/5/96

lsmith Laura Smith 3/12/96

jhsu Jake Hsu 4/17/96

jdoe John Doe 4/15/96

A particularly useful sort option is the -u option, which eliminates any duplicate entries in a file while ordering the file. For example, the file todays.logins:

sphillip

jchen

jdoe

lkeres

jmarsch

ageorge

lkeres

proy

jchen

shows a listing of each username that logged into the system today. If we want to know how many unique users logged into the system today, using sort with the -u option will list each user only once. (The command can then be piped into "wc -l" to get a number):

% sort -u todays.logins

ageorge

jchen

jdoe

jmarsch

lkeres

proy

sphillip


Introduction to Unix - 14 AUG 1996
[Next] [Previous] [Up] [Top] [Contents]