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
There are several useful options for sort that
are shown below
||Ignores any leading blank characters when sorting.
||Checks to make sure that the file is not already sorted. If it is, sort
will not display anything
||Sorts according to letters, digits and blanks (a "dictionary" sort)
||Case insensitive sort. Caps and lowercases are considered the same.
||Ignores non printable characters
||Defines the field to sort by. Note that field number one is "0"
||Defines the field to end the sort by
||Sorts as if the string is a month. Thus MAR is considered less than SEP
||Specifies a numeric key
||Specifies an output file to which to write matches
||Specifies a reverse sort
||Specifies the delimiter character for sorting fields
||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?
Table of Contents