There are several useful options for sort that are shown below
Option | Explanation |
---|---|
-b | Ignores any leading blank characters when sorting. |
-c | Checks to make sure that the file is not already sorted. If it is, sort will not display anything |
-d | Sorts according to letters, digits and blanks (a "dictionary" sort) |
-f | Case insensitive sort. Caps and lowercases are considered the same. |
-i | Ignores non printable characters |
+keybeg | Defines the field to sort by. Note that field number one is "0" |
-keyend | Defines the field to end the sort by |
-M | Sorts as if the string is a month. Thus MAR is considered less than SEP |
-n | Specifies a numeric key |
-ofile | Specifies an output file to which to write matches |
-r | Specifies a reverse sort |
-tfld-sep | Specifies the delimiter character for sorting fields |
-u | Makes sure that duplicate items appear only once. |
Consider the following examples
As we said, sometimes the quirks of ASCII can cause problems when sorting. You must remember that ASCII considers capitals before lower case letters. Thus, "Q" comes before "a" in ASCII thinking. Thus, you will often use the -f option to sort in the intuitive way:
Another very useful option is the field sorting option. Using a +[number] you can sort the file according to various fields. In the following example, we sort on last names instead of first names.
In the above example, sort used the space character as its field separator. You can also specify some other character as a delimiter such as in the following example in which we sort on the pipe (|). Notice that we also use the -n option to sort the field according to numerical value since ASCII sorting produces its usual strange output (i.e.: 129 comes before 28 because 129 starts with a 1).
Finally, I don't know if you remember, but on day one we were talking about pipelines and we gave the following as an example:
cat directory_listing | grep .html | sort | more
Well, I return to the commands because at this point you are finally prepared to interpret the command. Essentially, you are cat'ing a file that ostensibly holds a bunch of files in a directory (you might also use ls | grep....), parsing through it looking for html files, sorting those html files and making sure that sort only displays one screens worth of information at once. Pretty cool eh?