sort - sort or sequence check text files
sort [-bdfinruD] [-t str|-: regexp] [+pos1 [-pos2]] [-k pos1[,pos2]] [file ...]
The sort
program sorts the lines of one or more text files.
Comparisons are based on one or more sort keys extracted from each line of input.
If no sort keys are explicitly specified,
the entire lines are used.
By default,
the comparison is made lexicographically,
using the ordering specified by the current locale (if any).
The following global options control the operation of sort
:
-u
-t
STRING-:
REGEXP-D
The following options override the default ordering rules, and may also be attached to specific keys (see +pos1... and -k). When they appear independent of key field specifications, the requested field ordering rules are applied globally to all sort keys. When attached to a specific key, the ordering options override all global ordering options for that key.
-b
-d
-f
-i
-n
sort
,
-n implies -b (which matters if you use character position offsets).
With this version,
if you want -b,
use -b.-r
Finally, there are two, mutually confusing, ways of specifying sort keys:
1.2
specifies the third character of the second field.
If .N is omitted,
the position refers to the start of the field,
so 2
is equivalent to 2.0
.sort +1 -n -2
will produce an error.-k
POS1[,POS2]1.2
specifies the second character of the first field.
If the character offset of POS1 is omitted,
the position refers to the start of the field,
so -k 2,...
is equivalent to -k 2.1,...
.
If the character offset of POS2 is omitted,
the position refers to the end of the field.-k ...,2
is equivalent to -k ...,2.0
. sort +1 -2
sort -k 2,2
Either example sorts lexicographically by the second field of each line.
sort +1 -2 +3 -5
sort -k 2,2 -k 4,5
sort +1 -2 -k 4,5
Sorts lexicographically by the second, fourth and fifth field of each
line. (More verbosely, to compare two lines, we first compare their
second field. If the two second fields are lexicographically equal,
we compare the fourth field. If the fourth fields are equal, compare
the fifth field. If the fifth fields are also equal, the lines are
considered equal.)
sort -n +2 +0b -1
sort -n -k 3 -k 1b,1
Sorts numerically by the fields of each line starting from the third
(ie, 3rd, 4th, 5th, ...). If two lines compare as equal, compare the
first field lexicographically, ignoring any leading whitespace.
sort -n +2.1 -2.4
sort -n -k 3.2,3.4
Numerically compares the second through fourth characters of the third
field.
sort.pl -t: +2n /etc/passwd
Splits the lines of the file C</etc/passwd> into colon-separated
fields, and sorts numerically on fields starting from the third field.
sort -: '(\d+)' +3n -4 +2 -3 +1n -2
Separates the input into fields consisting alternately of either all
non-digits or all digits. (The regular expression instructs C<sort>
to use fields separated by fields of digits. Therefore, the first
field will be non-digits, but may be empty.) Sort numerically by
the fourth field (second numeric field), lexicographically by the
third field (second non-numeric field), and numerically by the second
field (first numeric field).
These environment variables affect the execution of sort
:
See locale for more information about localization and Perl.
No bugs in sort
are currently known.
Albert Dvornik, <bert@mit.edu>.
This program is copyright (c) Albert Dvornik 1999.
This program is free and open software. You may use, modify, distribute, and sell this program (and any modified variants) in any way you wish, provided you do not restrict others from doing the same.