UNIX in a Nutshell: System V Edition

UNIX in a Nutshell: System V EditionSearch this book
Previous: Reference: soelimChapter 2
Unix Commands
Next: Reference: sortbib
 

sort

sort [options] [files]

Sort the lines of the named files, typically in alphabetical order. See also uniq, comm, and join.

Options

-b

Ignore leading spaces and tabs.

-c

Check whether files are already sorted, and if so, produce no output.

-d

Sort in dictionary order (ignore punctuation).

-f

"Fold"; ignore uppercase/lowercase differences.

-i

Ignore nonprinting characters (those outside ASCII range 040-176).

-k fieldspec

Specify significance of input fields for sorting. See the fuller description below. Solaris only.

-m

Merge sorted input files.

-M

Compare first three characters as months.

-n

Sort in arithmetic (numerical) order.

-o file

Put output in file.

-r

Reverse the order of the sort.

-tc

Fields are separated with c (default is any whitespace).

-T dir

Use dir for temporary files. Solaris only.

-u

Identical lines in input file appear only one (unique) time in output.

-y[kmem]

Adjust the amount of memory (in kilobytes) sort uses. If kmem is not specified, allocate the maximum memory.

-zrecsz

Provide the maximum number of bytes for any one line in the file. This option prevents abnormal termination of sort in certain cases. Solaris sort accepts but otherwise ignores this option.

+n [-m]

Skip n fields before sorting, and sort up to field position m. If m is missing, sort to end of line. Positions take the form a.b, which means character b of field a. If .b is missing, sort at the first character of the field. Counting starts at zero. Solaris allows fields to have optional trailing modifiers, as in the -k option.

Field Specifications for -k

A fieldspec has the form fieldstart[type][,fieldend[type]].

fieldstart

A field number and optional starting character of the form fnum[.schar]. fnum is the field number, starting from 1. schar, if present, is the starting character within the field, also counting from 1.

fieldend

A field number and optional ending character of the form fnum[.echar]. fnum is the field number, starting from 1. echar, if present, is the last significant character within the field, also counting from 1.

type

A modifier, one of the letters b, d, f, i, M, n, or r. The effect is the same as the corresponding option, except that the b modifier only applies to the fields, not the whole line.

Examples

List files by decreasing number of lines:

wc -l * | sort -rn

Alphabetize a list of words, remove duplicates, and print the frequency of each word:

sort -fd wordlist | uniq -c

Sort the password file numerically by the third field (user ID):

sort +2n -t: /etc/passwd

Find the top 20 disk hogs on a system:

cd /home; du -sk * | sort -nr | head -20


Previous: Reference: soelimUNIX in a Nutshell: System V EditionNext: Reference: sortbib
Reference: soelimBook IndexReference: sortbib

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System