Полезная информация

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 48.10 Working with Names and Addresses Chapter 48
Office Automation
Next: 48.12 Using index with a Filter

48.11 The index Database Program

The index program is a flexible database management program. index is pretty neat: it works by keeping two files for each database, a field description file and a file of sorted data. It's also fairly intuitive to use, once you get started.

To use index, you should first set up a database directory. By default, index looks for the directory $HOME/.index.

% mkdir ~/.index

Then when you start up index for the first time, you're asked to select a name for the database you want to use.

Select a database:

If you specify the name of a database that doesn't exist yet, index assumes that you want to create it. index puts you in the editor specified in your EDITOR (6.3) environment variable so you can list the titles for each field in the new database. Up to 16 fields can be supported. For example, you can create a database called addresses, defined to have the following fields:


After exiting the editor, you are put into the main menu:

Database: addresses  (0 entries)

        a - Add new entry to database
        f - Find entry in database
        r - Read database entry by entry
        s - Save modifications, do not exit
        q - Save modifications, exit
        x - Exit


There are no entries in the new database. You can start adding entries now by pressing a.

When editing an entry in the database, you're prompted by each of the fields that were set up when the database was defined. After completing a field, press RETURN to get to the next field. There are also a limited number of escape sequences supported for editing and moving around - for example, CTRL-d to delete to the end of the line, or CTRL-p to move up a line. When you're done with the entire entry, press ESCAPE.

NAME:               Stella Rosenzweig
TITLE:              Mom
STREET ADDRESS:     2456 Bronx Park East
CITY:               Bronx
STATE:              NY
ZIP CODE:           10467
VOICE PHONE NUMBER: 718-231-2618

Once you've saved the new entry, you'll be back to the main menu. You can now add more entries, look at existing entries, search for a string in one of the existing entries, or exit.

To search for an existing entry, press f at the main menu. You'll be asked for the search pattern:

Pattern to search for: Ben

The index program searches all fields matching the string. Regular expressions (26.4) are also accepted in the search string. Each entry with the matching string is shown, one-by-one:

NAME:               Benjamin Braddock
TITLE:              Sales Manager
ORGANIZATION:       Acme Plastics, Inc.
STREET ADDRESS:     103 Morris St., Suite A
CITY:               Tucson
STATE:              AZ
ZIP CODE:           85472
VOICE PHONE NUMBER: 800-998-9938
FAX PHONE NUMBER:   800-999-9999
EMAIL ADDRESS:      ben@acme.com

      <RET> = next entry               "d" = delete this entry
      "-" = previous entry             "e" = edit this entry
      "q" = return to main menu


You can also search for strings on the index command line. To do so, specify both the database name and the string. (Enclose the search string in single quotes (8.14) if you want to use a regular expression.)

% index addresses '[bB]en'

All matching entries will be printed to standard output.

The most common use for a database like this is to maintain information about people. But you can use index to maintain all sorts of information. For example, you might keep one for information about local restaurants:

NAME:                           Rooster BBQ & Grill
STREET ADDRESS:                 1122 Rooster Ave.
CITY:                           Cambridge
PHONE:                          555-1212
RESERVATIONS (y,n):             n
FOOD (1=poor, 10=excellent):    8
DECOR (1=poor, 10=excellent):   2
SERVICE (1=poor, 10=excellent): 6
RECOMMENDED DISHES:             Baby-back ribs, Elvis pizza
STAY AWAY FROM:                 Pulled turkey plate, mango pie
RECOMMENDED BY:                 Jerry, Boston Phoenix

For even more flexibility, index can run a filter program (48.12).

- LM

Previous: 48.10 Working with Names and Addresses UNIX Power ToolsNext: 48.12 Using index with a Filter
48.10 Working with Names and Addresses Book Index48.12 Using index with a Filter

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System