[Introduction to UNIX for Web Developers]
[Spacer] [Table of Contents]
[Next Page]
[Previous Page]
[Spacer]
The "sort" Utility
Once you have found the files you are after using grep and find, you often have a need to manipulate those files in special ways. One popular tool for manipulating files is the "sort" utility. "sort" allows you to sort the lines of a file alphabetically (according to standard ASCII rules which means, for example, that capital letters come before lower case letters). Check out this basic example

[Sort]

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:

[Sort with -f]

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.

[Sort on fields]

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).

[Sort on fields]

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?

Previous | Next | Table of Contents