sort
From NetBSD Wiki
The sort(1) utility sorts text files by lines. Comparisons are based on one or more sort keys extracted from each line of input, and are performed lexicographically. By default, if keys are not given, sort regards each input line as a single field.
Contents |
Examples
% cat colors.txt red blue green
To sort colors.txt
% sort colors.txt blue green red
The -n option specifies sorting by numeric value. Thus recognizing numbers with more than one digit.
% cat numbers.txt 3 5 15 10
% sort numbers.txt 10 15 3 5
% sort -n numbers.txt 3 5 10 15
Tutorial
This is a basic sort tutorial
Data file contents. ( sf )
apple 33 boxes 4 sold 15.25 per pound pear 12 boxes 55 sold 1.30 per pound peach 3 boxes 499 sold 25.05 per pound banana 129 boxes 33 sold 0.12 per pound plum 96 boxes 104 sold 50.41 per pound kiwi 8 boxes 41 sold 9.99 per pound cherry 5 boxes 358 sold 12.04 per pound fig 155 boxes 204 sold 58.36 per pound
Simple sort on first column.
% sort sf apple 33 boxes 4 sold 15.25 per pound banana 129 boxes 33 sold 0.12 per pound cherry 5 boxes 358 sold 12.04 per pound fig 155 boxes 204 sold 58.36 per pound kiwi 8 boxes 41 sold 9.99 per pound peach 3 boxes 499 sold 25.05 per pound pear 12 boxes 55 sold 1.30 per pound plum 96 boxes 104 sold 50.41 per pound
Reverse it.
% sort -r sf plum 96 boxes 104 sold 50.41 per pound pear 12 boxes 55 sold 1.30 per pound peach 3 boxes 499 sold 25.05 per pound kiwi 8 boxes 41 sold 9.99 per pound fig 155 boxes 204 sold 58.36 per pound cherry 5 boxes 358 sold 12.04 per pound banana 129 boxes 33 sold 0.12 per pound apple 33 boxes 4 sold 15.25 per pound
( -r Reverse the sense of comparisons. )
Sort the second column.
% sort -k2n sf peach 3 boxes 499 sold 25.05 per pound cherry 5 boxes 358 sold 12.04 per pound kiwi 8 boxes 41 sold 9.99 per pound pear 12 boxes 55 sold 1.30 per pound apple 33 boxes 4 sold 15.25 per pound plum 96 boxes 104 sold 50.41 per pound banana 129 boxes 33 sold 0.12 per pound fig 155 boxes 204 sold 58.36 per pound
-n An initial numeric string, consisting of optional blank space, optional minus sign, and zero or more digits (including decimal point) is sorted by arithmetic value. (The -n option no longer implies the -b option.)
-k field1[,field2] Designates the starting position, field1, and optional ending position, field2, of a key field. The -k option replaces the obsolescent options +pos1 and -pos2.
Sort the sixth column in reverse.
% sort -k6r sf fig 155 boxes 204 sold 58.36 per pound plum 96 boxes 104 sold 50.41 per pound peach 3 boxes 499 sold 25.05 per pound apple 33 boxes 4 sold 15.25 per pound cherry 5 boxes 358 sold 12.04 per pound kiwi 8 boxes 41 sold 9.99 per pound pear 12 boxes 55 sold 1.30 per pound banana 129 boxes 33 sold 0.12 per pound
Create an 'inventory' file.
-o output The argument given is the name of an output file to be used instead of the standard output. This file can be the same as one of the input files.
% sort -k2n -o inventory sf % cat inventory peach 3 boxes 499 sold 25.05 per pound cherry 5 boxes 358 sold 12.04 per pound kiwi 8 boxes 41 sold 9.99 per pound pear 12 boxes 55 sold 1.30 per pound apple 33 boxes 4 sold 15.25 per pound plum 96 boxes 104 sold 50.41 per pound banana 129 boxes 33 sold 0.12 per pound fig 155 boxes 204 sold 58.36 per pound
Top seller.
% sort -k4r -o top-seller sf % cat top-seller peach 3 boxes 499 sold 25.05 per pound cherry 5 boxes 358 sold 12.04 per pound fig 155 boxes 204 sold 58.36 per pound plum 96 boxes 104 sold 50.41 per pound pear 12 boxes 55 sold 1.30 per pound kiwi 8 boxes 41 sold 9.99 per pound banana 129 boxes 33 sold 0.12 per pound apple 33 boxes 4 sold 15.25 per pound
Caveats
By default, a field consists of the visible field text, but including the preceding separators. That means that the sort order is not what you usually expect. Consider the following file:
$ cat > demofile a 1 aa 11 aaa 111 aaaa 1111 a 1111 aa 111 aaa 11 aaaa 1
When you sort this file using the second field as the key, you get the following:
$ sort -k2,2 demofile a 1 aa 11 aaaa 1 aaa 11 aa 111 a 1111 aaa 111 aaaa 1111
Look at lines 2 and 3, which are intuitively ordered wrong. But when you count the number of space characters, you see that line 2 has more of them, and since the space character has a very low collating value, it is ordered first. To get what you want, you have to add the -b option. (Be sure to specify -k as the very last option, if you want your program to be portable.)
$ sort -bk2,2 demofile a 1 aaaa 1 aa 11 aaa 11 aaa 111 aa 111 aaaa 1111 a 1111
