Creating and Sorting Lists of Data





Creating and Sorting Lists of Data

Database-like storage and search-oriented retrieval functions are also common day-to-day productivity uses for personal workstations. From the command line, you can accomplish these types of tasks using editors such as vi and emacs, plaintext files, and a set of two simple commands: sort, which is used to sort plaintext data into alphanumeric order, and grep, which is used to search text data for a specific word or phrase.

Creating Searchable or Sortable Lists

The art of creating useful plaintext databases with vi or emacs revolves mainly around the ability to use spaces or tabs effectively to organize information into single lines broken into multiple single-word columns. To create a plaintext database, start your favorite text editor and follow these steps:

1.
Logically (that is, in your imagination) separate the information you would like to organize into fields a single word long. For example, for a companywide list of phone numbers, your fields might be last name, first name, area code, phone number, and department.

2.
Enter your first record on a single text line, one field at a time, inserting tabs between them.

3.
Enter the rest of your records the same way, one on each line, until all your records are represented in the file.

To continue with the phone number list example, imagine a list of phone numbers in a file called phones.txt, as shown in Listing 22.2.

A Simple Text-Based Database of Phone Numbers
Rasmussen    Jake   800   111-1111    Heating
Larsen       Eve    800   222-2222    Heating
Amberson     Laura  888   111-9999    Cooling
Swenson      Celia  800   666-6666    Operations
Wagoner      Shane  888   232-2323    Cooling
Filipanteng  Lee    800   696-9696    Heating

Following the format we imagined earlier, this list of phone numbers has five columns: last name, first name, area code, phone number, and department, in that order. The names aren't in any particular order; they've just been entered into the plaintext file phones.txt using a text editor as each new person joined the company.

As simple as this file appears, it's already a database. Let's examine the ways in which this data can be used.

Displaying Specific Entries

In Chapter 19, "Performing Basic Shell Tasks," you learned how to use the grep command to search for various kinds of text; grep is also useful for finding and displaying a specific item in a plaintext database. Suppose you want a listing of all the people in the company phone list who work in the Cooling department. To do that, you call the grep command, supplying the word Cooling and the name of the database file as arguments:

[[email protected] you]$ grep Cooling phones.txt
Amberson    Laura  888   111-9999    Cooling
Wagoner     Shane  888   232-2323    Cooling
[[email protected] you]$

As you can see, you easily take care of this request. Suppose, on the other hand, that you want to find the phone number for Eve Larsen and the department where she works. To search for Eve's last name, Larsen, call the grep command, supplying the word Larsen and the name of the database file as arguments:

[[email protected] you]$ grep Larsen phones.txt
Larsen     Eve   800   222-2222    Heating
[[email protected] you]$

You also accomplish this request easily. Searches of this kind might not be impressive to you on a short list of six phone numbers; the same technique will work equally well, however, on a plaintext database that is several thousand entries long.

Sorting List Data

Sometimes being able to sort data in a list is helpful. The sort command is designed for just such an occasion; sort simply accepts a plaintext listing and rearranges the lines into alphanumeric order. The simplest way to call sort is to use the name of a text file to sort as an argument on the command line. Here, you sort the phone number list from previous examples:

[[email protected] you]$ sort phones.txt
Amberson      Laura  888   111-9999    Cooling
Filipanteng   Lee    800   696-9696    Heating
Larsen        Eve    800   222-2222    Heating
Rasmussen     Jake   800   111-1111    Heating
Swenson       Celia  800   666-6666    Operations
Wagoner       Shane  888   232-2323    Cooling
[[email protected] you]$

Because the surname of each individual is in the first column in your list, you now have a list displayed in alphabetical order by last name. In fact, if you were going to print a long list of phone numbers formatted this way as a phone book, the command to generate the output would be as simple as

[[email protected] you]$ sort phones.txt | lpr

Let's assume for a moment, however, that you want to sort based on first names, rather than last names. The sort command enables you to alter the sorting criteriaor more specifically, the columnon which it will operate by using the -k option. To use the k option, follow the call to sort with the k option and the number of the column you want to use as the basis for your sort. Here, you sort the list of phone numbers based on the second column of informationthe first names of the employees in the list:

[[email protected] you]$ sort -k 2 phones.txt
Swenson       Celia  800   666-6666    Operations
Larsen        Eve    800   222-2222    Heating
Rasmussen     Jake   800   111-1111    Heating
Amberson      Laura  888   111-9999    Cooling
Filipanteng   Lee    800   696-9696    Heating
Wagoner       Shane  888   232-2323    Cooling
[[email protected] you]$

Suppose you want to group employees by department or by area code. To do that, you type the sort command, the k option, and the number of the column that holds department names, or the area codes, followed by the name of the file to be sorted:

[[email protected] you]$ sort -k 5 phones.txt
Amberson      Laura  888   111-9999    Cooling
Wagoner       Shane  888   232-2323    Cooling
Filipanteng   Lee    800   696-9696    Heating
Larsen        Eve    800   222-2222    Heating
Rasmussen     Jake   800   111-1111    Heating
Swenson       Celia  800   666-6666    Operations
[[email protected] you]$ sort -k 3 phones.txt
Rasmussen     Jake   800   111-1111    Heating
Larsen        Eve    800   222-2222    Heating
Swenson       Celia  800   666-6666    Operations
Filipanteng   Lee    800   696-9696    Heating
Amberson      Laura  888   111-9999    Cooling
Wagoner       Shane  888   232-2323    Cooling
[[email protected] you]$

As a final example, you can get a listing of only those employees who work in Heating, sort the list alphabetically by first name, and then send the listing to the printer. This can be done by calling grep to search the file phones.txt for the word Heating, sending that output through a pipe to the sort command with an option to sort based on the second column, and then sending that output through a pipe to the lpr command, which prints the data:

[[email protected] you]$ grep Heating phones.txt | sort -k 2 | lpr
[[email protected] you]$

More information on the sort command can be found in the man page for sort.


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows