eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak


Introduction to UNIX for Web Developers
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


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