Module 122 sort

Previous Table of Contents Next


Module 122
sort

DESCRIPTION

The external sort command sorts ASCII data. The input can be from files or the standard input. The output can be a file or the standard output. If multiple input files are given, the data from each file is merged during the sort.

COMMAND FORMAT

Following is the general format of the sort command.

 sort [ - ] [ -cmu ] [ -ooutput ] [ -ykmem ] [ -zrecsz ] [ -dfiMnr ] \           [ -btx ] [ +pos1 [ -pos2 ] ] [ file_list ] 

BSD (Berkeley)
sort [ - ] [ -cmu ] [ -ooutput ] [ -dfinr ] [ -btx ] \
[ +pos1 [ -pos2 ] ] [ -Tdirectory ] [ file_list ]

Options

The following list describes the options and their arguments that may be used to control how sort functions.

- Forces sort to read from the standard input. Useful for reading from pipes and files simultaneously .
-c Verifies that the input is sorted according to the other options specified on the command line. If the input is sorted correctly then no output is provided. If the input is not sorted then sort informs you of the situation. The message resembles this.
 sort: disorder: This line not in sorted order. 
-m Merges the sorted input. sort assumes the input is already sorted. sort normally merges input as it sorts. This option informs sort that the input is already sorted, thus sort runs much faster.
-o output Sends the output to file output instead of the standard output. The output file may be the same name as one of the input files.
-u Suppress all but one occurrence of matching keys. Normally, the entire line is the key. If field or character keys are specified, then the suppressing is done based on the keys.
-y kmem Use kmem kilobytes of main memory to initially start the sorting. If more memory is needed, sort automatically requests it from the operating system. The amount of memory allocated for the sort impacts the speed of the sort significantly. If no kmem is specified, sort starts with the default amount of memory (usually 32K). The maximum (usually 1 Megabyte) amount of memory may be allocated if needed. If 0 is specified for kmem , the minimum (usually 16K) amount of memory is allocated.
-z recsz Specifies the record size used to store each line. Normally the recsz is set to the longest line read during the sort phase. If the -c or -m options are specified, the sort phase is not performed and thus the record size defaults to a system size. If this default size is not large enough, sort may abort during the merge phase. To alleviate this problem you can specify a recsz that will allow the merge phase to run without aborting.

Ordering Rule Options

-d Specifies dictionary sort. Only blanks, digits, and alphabetic characters are significant in the comparison.
-f Fold lowercase letters to uppercase. Ignores the significance of upper and lowercase ASCII characters.
-i Ignore characters outside the ASCII range of 040 (octal) and 0176 (octal). Only alphabetic characters, blanks, digits, and punctuation are used for comparison (printable characters). Control characters are ignored. This is only valid for nonnumeric sorts.
-M Compare fields as months. The first three nonblank characters are folded (see -i option) to uppercase and compared. Thus January is compared as JAN. JAN precedes FEB, and fields not containing months precede JAN. The -b option is placed in effect automatically.
-n Sorts the input numerically . The comparison is based on numerical value instead of alphabetic value. The number field used for comparison can contain optional blanks, optional minus signs, optional decimal point, and zero or more digits. The -b option is placed in effect automatically. Exponential numbers are not sorted correctly.
-r Reverse the order of the output.

Restricted Sort Key Options

+ pos1 Specifies the beginning position of the input line used for field comparison. If pos1 is not specified then comparison begins at the beginning of the line. The pos1 position has the notation of f.c . The f specifies the number of fields to skip. The c specifies the number of characters to skip. For example, 3.2 is interpreted as skip three fields and two characters before performing comparisons. Omitting the . c portion is equivalent to specifying .0. Field one is referred to as position 0. If f is set to 0 then character positions are used for comparison.
- pos2 Specifies the ending position of the input line used for field comparison. If pos2 is not specified then comparison is done through the end of the line. The pos2 position has the notation of f.c . The f specifies to compare through field f . The c specifies the number of characters to compare through after field f . For example, -4.3 is interpreted as compare through three characters after the end of field four. Omitting the . c portion is equivalent to specifying .0.
-b Ignores leading blanks when using the restricted sort key positions (+ pos1 and - pos2 ). If the -b option is placed before the first + pos1 argument, then it applies to all + pos1 arguments. The -b option can be attached to each pos string to affect only that field.
-t c Use character c as the field separator. Multiple c s may be specified to represent empty fields surrounded by separators.

Both these options and any of the ordering rule options (d, f, i, M, n, and r) can be attached to position arguments. For example,

 cj> sort -t: +2.0n -3.0 +0.0bdf -1.0 /etc/passwd 


Previous Table of Contents Next

Copyright Wordware Publishing, Inc.


Illustrated UNIX System V
Illustrated Unix System V/Bsd
ISBN: 1556221878
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Robert Felps

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net