📄 ch48_10.htm
字号:
# loop to read the record_template # field_inc = the number of fields # fields[] = an array of labels indexed by position field_inc=0 while ((getline < record_template) > 0) { ++field_inc fields[field_inc] = $1 } field_tot=field_inc}# Now we are reading the records# Print filename for each new recordFNR == 1 { field_inc=0 print "\n" FILENAME ":"}{ # Print the field's position, label and value # The last field can have any number of lines without a label. if (++field_inc <= field_tot){ if (field_inc >= 10) space = ". " else space = ". " print field_inc space fields[field_inc] ":\t" $NF }else print $NF }' $*</PRE></BLOCKQUOTE></P><PCLASS="para">Note that the program is notdoing any input validation. If the record is missinga Division name (and you didn't leave the fourth lineblank), whatever is on line 4 will match up with Division,even if it's really a street address.One of the uses of <EMCLASS="emphasis">read.base</EM> is simply to verifythat what you entered in the file is correct.</P><PCLASS="para">If you specify more than one record, then you willget all of those records output in the order thatyou specified them on the command line.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-58267"></A>The second program is <EMCLASS="emphasis">mail.base</EM>.It extractsmailing label information.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>mail.base pmui</B></CODE>Peter MuiInternational Sales ManagerO'Reilly & Associates, Inc.East Coast Division90 Sherman StreetCambridge, MA 01240</PRE></BLOCKQUOTE></P><PCLASS="para">If you supply a <EMCLASS="emphasis">record-list</EM>, then you will get a listof mailing labels. </P><PCLASS="para">Here is the <EMCLASS="emphasis">mail.base</EM> program:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">nawk 'BEGIN { FS="\n"; # test that user supplies a record if (ARGC < 2) { print "Please supply record list on command line" exit }}# ignore blank lines/^$/ { next }# this is hard-coded to record format;# print first 5 fields and then print# city, state zip on one line.{ if (FNR < 6) print $0 else if (FNR == 6) printf $0 ", " else if (FNR == 7) printf $0 else if (FNR == 8) printf " " $0 "\n\n"}' $*</PRE></BLOCKQUOTE></P><PCLASS="para">Variations on this very simple program can be written to extract or compile other pieces of information. You could also outputformatting codes used when printing the labels.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-58281"></A>The last program is <EMCLASS="emphasis">list.base</EM>.It prepares a tabularlist of names and records and allows you to select aparticular record.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>list.base lwalsh pmui jberlin</B></CODE> # NAME & COMPANY FILE 1. Linda Walsh, O'Reilly & Associates, Inc. lwalsh 2. Peter Mui, O'Reilly & Associates, Inc. pmui 3. Jill Berlin, O'Reilly & Associates, Inc. jberlin Select a record by number: 2</PRE></BLOCKQUOTE></P><PCLASS="para">When you select the record number, thatrecord is displayed by using <EMCLASS="emphasis">read.base</EM>. I have not built in anypaging capability, so the list will scroll continuouslyrather than pause after 24 lines or so as it might.</P><PCLASS="para">Here is the <EMCLASS="emphasis">list.base</EM> program:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">nawk 'BEGIN { # Do everything as BEGIN procedure # test that user supplied record-list if (ARGC < 2) { print "Please supply record list on command line" exit } # Define report format string in one place. FMTSTR = "%3s %-40s %-15s\n" # print report header printf(FMTSTR, "#","NAME & COMPANY", "FILE") # For each record, get Name, Title and Company and print it. inc=0 for (x=1; x < ARGC; x++){ getline NAME < ARGV[x] getline TITLE < ARGV[x] getline COMPANY < ARGV[x] record_list[x] = ARGV[x] printf(FMTSTR, ++inc ".", NAME ", " COMPANY, ARGV[x]) } # Prompt user to select a record by number printf "Select a record by number:" getline answer < "-" # Call read.base program to display the selected record system("read.base " record_list[answer])}' $*</PRE></BLOCKQUOTE></P><PCLASS="para"> Different versions of this program can be written to examineindividual pieces of information across a set of records.</P><PCLASS="para">Article<ACLASS="xref"HREF="ch45_22.htm"TITLE="Handling Files Line-by-Line ">45.22</A>shows how to write a shell script that creates a prompt-driven frontend to collect names and addresses.(It needs to be modified to put out a blank line for empty fieldsand not to write the labels into the file.)<ACLASS="indexterm"NAME="AUTOID-58296"></A><ACLASS="indexterm"NAME="AUTOID-58297"></A><ACLASS="indexterm"NAME="AUTOID-58298"></A></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DD</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><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"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></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><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">48.9 PostScript Calendars with pcal </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">48.11 The index Database Program </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -