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.

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:

 [you@workstation20 you]$ grep Cooling phones.txt Amberson    Laura  888   111-9999    Cooling Wagoner     Shane  888   232-2323    Cooling [you@workstation20 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:

 [you@workstation20 you]$ grep Larsen phones.txt Larsen     Eve   800   222-2222    Heating [you@workstation20 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:

 [you@workstation20 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 [you@workstation20 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

 [you@workstation20 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:

 [you@workstation20 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 [you@workstation20 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:

 [you@workstation20 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 [you@workstation20 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 [you@workstation20 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:

 [you@workstation20 you]$ grep Heating phones.txt | sort -k 2 | lpr [you@workstation20 you]$ 

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



    SAMS Teach Yourself Red Hat(r) Fedora(tm) 4 Linux(r) All in One
    Cisco ASA and PIX Firewall Handbook
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 311
    Authors: David Hucaby

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net