📄 ch48_10.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 48] 48.10 Working with Names and Addresses </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-10-23T15:55:35Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch48_01.htm"TITLE="48. Office Automation"><LINKREL="prev"HREF="ch48_09.htm"TITLE="48.9 PostScript Calendars with pcal "><LINKREL="next"HREF="ch48_11.htm"TITLE="48.11 The index Database Program "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch48_09.htm"TITLE="48.9 PostScript Calendars with pcal "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 48.9 PostScript Calendars with pcal "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 48<BR>Office Automation</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch48_11.htm"TITLE="48.11 The index Database Program "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 48.11 The index Database Program "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7780">48.10 Working with Names and Addresses </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-7780-IX-ADDRESS-DATABASES-CREATED-USING-AWK"></A><ACLASS="indexterm"NAME="UPT-ART-7780-IX-DATABASES-CREATED-USING-AWK"></A><ACLASS="indexterm"NAME="UPT-ART-7780-IX-NAWK-UTILITY-DATABASES-CREATED-USING"></A>One of the simplest applications of<SPANCLASS="link"><EMCLASS="emphasis">awk</EM> (<ACLASS="linkend"HREF="ch33_11.htm"TITLE="Quick Reference: awk ">33.11</A>)</SPAN>is building a name and address database.It is a good exercisefor learning <EMCLASS="emphasis">awk</EM> as well. It involves organizingthe information as a record and then writing programsthat extract information from the records for displayin reports.The scripts in this article use<SPANCLASS="link"><EMCLASS="emphasis">nawk</EM> (<ACLASS="linkend"HREF="ch33_12.htm"TITLE="Versions of awk ">33.12</A>)</SPAN>instead of <EMCLASS="emphasis">awk</EM>, but the principles are the same.</P><PCLASS="para">The first thing to decide is the structure of a record.At the very least we'd like to have the following fields:</P><BLOCKQUOTECLASS="literallayout"><PCLASS="literallayout">Name<BR>Street<BR>City<BR>State<BR>Zip</P></BLOCKQUOTE><PCLASS="para">But we may wish to have a more complex record structure:</P><BLOCKQUOTECLASS="literallayout"><PCLASS="literallayout">Name<BR>Title<BR>Company<BR>Division<BR>Street<BR>City<BR>State<BR>Zip<BR>Phone<BR>Fax<BR>Email<BR>Directory<BR>Comments</P></BLOCKQUOTE><PCLASS="para">It doesn't matter to our programming effort whether therecord has five fields or thirteen. It does matter thatthe structure is decided upon before you begin programming. </P><PCLASS="para">The next decision we must make is how to distinguishone field from the next and how to distinguish one recordfrom another. If your records are short, you could haveone record per line and use an oddball character as a fielddelimiter:</P><PCLASS="para"><BLOCKQUOTECLASS="literallayout"><PCLASS="literallayout">Name~Street~City~State~Zip<BR>Name1~Street1~City1~State1~Zip1</P></BLOCKQUOTE></P><PCLASS="para">The downside of this solution is that it can be difficultto edit the records. (We are going to try to avoid writingprograms for automating data entry. Instead, we will assumethat you create the record with a text editor-<EMCLASS="emphasis">vi</EM> or Emacs,for example.)</P><PCLASS="para">Another solution is to put each field on a line by itselfand separate the records with a blank line: </P><BLOCKQUOTECLASS="literallayout"><PCLASS="literallayout">Name<BR>Street<BR>City<BR>State<BR>Zip<BR><BR>Name1<BR>Street1<BR>City1<BR>State1<BR>Zip1</P></BLOCKQUOTE><PCLASS="para">This is a good solution. You have to be careful that thedata does not itself contain blank lines. For instance,if you wanted to add a field for Company name, and notall records have a value for Company, then you must usea placeholder character to indicate an empty value.</P><PCLASS="para">Another solution is to put each record in its own file and put each field on its own line. This is the record organizationwe will implement for our program. Two advantagesof it are that it permits variable length records and it doesnot require the use of special delimiter characters. Itis therefore pretty easy to create or edit a record. It isalso very easy to select a subset of records for processing.</P><PCLASS="para">We will give each file a name that uniquely identifies it in the current directory.A list of records is the same as a list of files.Here is a sample record in a file named <EMCLASS="emphasis">pmui</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">Peter MuiInternational Sales ManagerO'Reilly & Associates, Inc.East Coast Division90 Sherman StreetCambridgeMA01240617-354-5800617-661-1116peter@ora.com/home/peterAny number of lines may appear as a comment.</PRE></BLOCKQUOTE></P><PCLASS="para">In this record, there are thirteen fields, any of which can be blank (butthe blank line must be there to save the position), and thelast field can have as many lines as needed. </P><PCLASS="para">Our record does not contain labels that identify what eachfield contains. While we could put that information inthe record itself, it is better to maintain the labels separatelyso they can be changed in a single location.(You can create a record template thatcontains the labelsto help you identify fields when adding a new record.)</P><PCLASS="para">We have put the labels for these fields in a separatefile named <EMCLASS="emphasis">dict</EM>. We won't show this file because its contents describe the record structure as shown above.</P><PCLASS="para">We are going to have three programs and they share the samesyntax:</P><BLOCKQUOTECLASS="literallayout"><PCLASS="literallayout"><CODECLASS="replaceable"><I>command record-list</I></CODE></P></BLOCKQUOTE><PCLASS="para">The <CODECLASS="replaceable"><I>record-list</I></CODE> is a list of one or more filenames. Youcan use wildcard characters, of course, on the command line tospecify multiple records.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-58251"></A>The first program, <EMCLASS="emphasis">read.base</EM>,reads the <EMCLASS="emphasis">dict</EM> fileto get the labels and outputs a formatted record.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>read.base record</B></CODE>pmui:1. Name: Peter Mui2. Title: International Sales Manager3. Company: O'Reilly & Associates, Inc.4. Division: East Coast Division5. Street: 90 Sherman Street6. City: Cambridge7. State: MA8. Zip: 012409. Phone: 617-354-580010. Fax: 617-661-111611. Email: peter@ora.com12. Directory: /home/peter13. Comments: Any number of lines may appear as a comment.</PRE></BLOCKQUOTE></P><PCLASS="para"><EMCLASS="emphasis">read.base</EM> first outputs the record name and thenlists each field.Let's look at <EMCLASS="emphasis">read.base</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">nawk 'BEGIN { FS=":" # test to see that at least one record was specified if (ARGC < 2) { print "Please supply record list on command line" exit } # name of local file containing field labels: record_template = "dict"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -