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

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 48.11 The index Database Program Chapter 48
Office Automation
Next: 49. Working with Numbers
 

48.12 Using index with a Filter

A particularly useful feature of the index (48.11) program is that you can use the -f option to filter the output. When a filter is used, the index program produces output in TAB-separated columns. Instead of listing each matching entry individually, the field titles are shown at the top, and each entry then appears underneath, displayed horizontally, with TABs between fields:

% index -f 'sed s/Mui/Mud/' addresses Peter
NAME    TITLE   ORGANIZATION    STREET ADDRESS  CITY    STATE   ...
Henry K Smith           Peter Johnson & Associates      324 Bur ...
Peter Mud       International Sales Manager     O'Reilly and As ...
Peter L. Loos   President       Introspective Solutions, Inc.   ...

The TAB-separated fields don't line up properly on your screen, but they make it convenient to manipulate columns using cut (35.14), awk (33.11), or perl (37.1). For example, you can use the cut command as your filter to limit the output to a few significant fields:

% index -f 'cut -f1,8,10' addresses '.'
NAME    VOICE PHONE NUMBER      EMAIL ADDRESS
Henry K Smith   617-555-1212    henry@pja
Paul S. Spencer        617-693-1111    paul@lotus.com
Peter Mui       800-998-9938    peter@ora.com

(We use '.' as the search string to match all entries.)

For your convenience, you can write up a shell script as a filter and place it in your $HOME/.index directory with a .fmt suffix. This feature comes in useful for particularly complicated filter programs. For example, if you want to be able to read the output of the previous example properly, you can try using awk as shown in article 35.22, or you can just use tbl (43.15) and nroff (43.13). To do this in a single step, try writing a filter [Linda is a great typist; she uses cat >> (25.2) to write short shell scripts (and show the script at the same time). A text editor like Emacs or vi will do fine, too, of course. -JP ]:

~ 


chmod +x 





% cat >> /.index/printinfo.fmt
#! /bin/sh
cut -f1,8,10 | sed '
1i\
.TS\
l l l.
$a\
.TE' | tbl | nroff | col
[CTRL-d]
% chmod +x ~/.index/printinfo.fmt
% index -f printinfo addresses '.'
NAME                VOICE PHONE NUMBER   EMAIL ADDRESS
Henry K Smith       617-555-1212         henry@pja
Paul S. Spencer     617-693-1111         paul@lotus.com
Peter Mui           800-998-9938         peter@ora.com

- LM


Previous: 48.11 The index Database Program UNIX Power ToolsNext: 49. Working with Numbers
48.11 The index Database Program Book Index49. Working with Numbers

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