\unnumbchapentry {Preface}{1} \unnumbsecentry{History of \code {awk} and \code {gawk}}{1} \unnumbsecentry{The GNU Project and This Book}{1} \unnumbsecentry{Acknowledgements}{3} \chapentry {Introduction}{1}{5} \secentry {Using This Book}{1}{1}{5} \unnumbsubsecentry{Dark Corners}{6} \secentry {Typographical Conventions}{1}{2}{6} \secentry {Data Files for the Examples}{1}{3}{7} \chapentry {Getting Started with \code {awk}}{2}{9} \secentry {A Rose By Any Other Name}{2}{1}{9} \secentry {How to Run \code {awk} Programs}{2}{2}{10} \subsecentry {One-shot Throw-away \code {awk} Programs}{2}{2}{1}{10} \subsecentry {Running \code {awk} without Input Files}{2}{2}{2}{11} \subsecentry {Running Long Programs}{2}{2}{3}{11} \subsecentry {Executable \code {awk} Programs}{2}{2}{4}{12} \subsecentry {Comments in \code {awk} Programs}{2}{2}{5}{13} \secentry {A Very Simple Example}{2}{3}{13} \secentry {An Example with Two Rules}{2}{4}{14} \secentry {A More Complex Example}{2}{5}{15} \secentry {\code {awk} Statements Versus Lines}{2}{6}{16} \secentry {Other Features of \code {awk}}{2}{7}{18} \secentry {When to Use \code {awk}}{2}{8}{18} \chapentry {Useful One Line Programs}{3}{21} \chapentry {Regular Expressions}{4}{23} \secentry {How to Use Regular Expressions}{4}{1}{23} \secentry {Escape Sequences}{4}{2}{24} \secentry {Regular Expression Operators}{4}{3}{26} \secentry {Additional Regexp Operators Only in \code {gawk}}{4}{4}{31} \secentry {Case-sensitivity in Matching}{4}{5}{33} \secentry {How Much Text Matches?}{4}{6}{34} \secentry {Using Dynamic Regexps}{4}{7}{35} \chapentry {Reading Input Files}{5}{37} \secentry {How Input is Split into Records}{5}{1}{37} \secentry {Examining Fields}{5}{2}{40} \secentry {Non-constant Field Numbers}{5}{3}{41} \secentry {Changing the Contents of a Field}{5}{4}{42} \secentry {Specifying How Fields are Separated}{5}{5}{44} \subsecentry {The Basics of Field Separating}{5}{5}{1}{44} \subsecentry {Using Regular Expressions to Separate Fields}{5}{5}{2}{45} \subsecentry {Making Each Character a Separate Field}{5}{5}{3}{46} \subsecentry {Setting \code {FS} from the Command Line}{5}{5}{4}{47} \subsecentry {Field Splitting Summary}{5}{5}{5}{48} \secentry {Reading Fixed-width Data}{5}{6}{49} \secentry {Multiple-Line Records}{5}{7}{51} \secentry {Explicit Input with \code {getline}}{5}{8}{53} \subsecentry {Introduction to \code {getline}}{5}{8}{1}{54} \subsecentry {Using \code {getline} with No Arguments}{5}{8}{2}{54} \subsecentry {Using \code {getline} Into a Variable}{5}{8}{3}{55} \subsecentry {Using \code {getline} from a File}{5}{8}{4}{56} \subsecentry {Using \code {getline} Into a Variable from a File}{5}{8}{5}{56} \subsecentry {Using \code {getline} from a Pipe}{5}{8}{6}{57} \subsecentry {Using \code {getline} Into a Variable from a Pipe}{5}{8}{7}{58} \subsecentry {Summary of \code {getline} Variants}{5}{8}{8}{58} \chapentry {Printing Output}{6}{61} \secentry {The \code {print} Statement}{6}{1}{61} \secentry {Examples of \code {print} Statements}{6}{2}{62} \secentry {Output Separators}{6}{3}{63} \secentry {Controlling Numeric Output with \code {print}}{6}{4}{64} \secentry {Using \code {printf} Statements for Fancier Printing}{6}{5}{64} \subsecentry {Introduction to the \code {printf} Statement}{6}{5}{1}{65} \subsecentry {Format-Control Letters}{6}{5}{2}{65} \subsecentry {Modifiers for \code {printf} Formats}{6}{5}{3}{66} \subsecentry {Examples Using \code {printf}}{6}{5}{4}{68} \secentry {Redirecting Output of \code {print} and \code {printf}}{6}{6}{70} \secentry {Special File Names in \code {gawk}}{6}{7}{72} \secentry {Closing Input and Output Files and Pipes}{6}{8}{74} \chapentry {Expressions}{7}{77} \secentry {Constant Expressions}{7}{1}{77} \subsecentry {Numeric and String Constants}{7}{1}{1}{77} \subsecentry {Regular Expression Constants}{7}{1}{2}{77} \secentry {Using Regular Expression Constants}{7}{2}{78} \secentry {Variables}{7}{3}{79} \subsecentry {Using Variables in a Program}{7}{3}{1}{79} \subsecentry {Assigning Variables on the Command Line}{7}{3}{2}{80} \secentry {Conversion of Strings and Numbers}{7}{4}{81} \secentry {Arithmetic Operators}{7}{5}{82} \secentry {String Concatenation}{7}{6}{83} \secentry {Assignment Expressions}{7}{7}{84} \secentry {Increment and Decrement Operators}{7}{8}{86} \secentry {True and False in \code {awk}}{7}{9}{87} \secentry {Variable Typing and Comparison Expressions}{7}{10}{88} \secentry {Boolean Expressions}{7}{11}{91} \secentry {Conditional Expressions}{7}{12}{92} \secentry {Function Calls}{7}{13}{93} \secentry {Operator Precedence (How Operators Nest)}{7}{14}{94} \chapentry {Patterns and Actions}{8}{97} \secentry {Pattern Elements}{8}{1}{97} \subsecentry {Kinds of Patterns}{8}{1}{1}{97} \subsecentry {Regular Expressions as Patterns}{8}{1}{2}{97} \subsecentry {Expressions as Patterns}{8}{1}{3}{98} \subsecentry {Specifying Record Ranges with Patterns}{8}{1}{4}{99} \subsecentry {The \code {BEGIN} and \code {END} Special Patterns}{8}{1}{5}{100} \subsubsecentry {Startup and Cleanup Actions} {8}{1}{5}{1} {100} \subsubsecentry {Input/Output from \code {BEGIN} and \code {END} Rules} {8}{1}{5}{2} {102} \subsecentry {The Empty Pattern}{8}{1}{6}{102} \secentry {Overview of Actions}{8}{2}{102} \chapentry {Control Statements in Actions}{9}{105} \secentry {The \code {if}-\code {else} Statement}{9}{1}{105} \secentry {The \code {while} Statement}{9}{2}{106} \secentry {The \code {do}-\code {while} Statement}{9}{3}{106} \secentry {The \code {for} Statement}{9}{4}{107} \secentry {The \code {break} Statement}{9}{5}{109} \secentry {The \code {continue} Statement}{9}{6}{110} \secentry {The \code {next} Statement}{9}{7}{111} \secentry {The \code {nextfile} Statement}{9}{8}{112} \secentry {The \code {exit} Statement}{9}{9}{112} \chapentry {Built-in Variables}{10}{115} \secentry {Built-in Variables that Control \code {awk}}{10}{1}{115} \secentry {Built-in Variables that Convey Information}{10}{2}{117} \secentry {Using \code {ARGC} and \code {ARGV}}{10}{3}{120} \chapentry {Arrays in \code {awk}}{11}{123} \secentry {Introduction to Arrays}{11}{1}{123} \secentry {Referring to an Array Element}{11}{2}{124} \secentry {Assigning Array Elements}{11}{3}{125} \secentry {Basic Array Example}{11}{4}{125} \secentry {Scanning All Elements of an Array}{11}{5}{127} \secentry {The \code {delete} Statement}{11}{6}{128} \secentry {Using Numbers to Subscript Arrays}{11}{7}{129} \secentry {Using Uninitialized Variables as Subscripts}{11}{8}{129} \secentry {Multi-dimensional Arrays}{11}{9}{130} \secentry {Scanning Multi-dimensional Arrays}{11}{10}{132} \chapentry {Built-in Functions}{12}{133} \secentry {Calling Built-in Functions}{12}{1}{133} \secentry {Numeric Built-in Functions}{12}{2}{134} \secentry {Built-in Functions for String Manipulation}{12}{3}{135} \secentry {Built-in Functions for Input/Output}{12}{4}{144} \secentry {Functions for Dealing with Time Stamps}{12}{5}{146} \chapentry {User-defined Functions}{13}{151} \secentry {Function Definition Syntax}{13}{1}{151} \secentry {Function Definition Examples}{13}{2}{152} \secentry {Calling User-defined Functions}{13}{3}{154} \secentry {The \code {return} Statement}{13}{4}{156} \chapentry {Running \code {awk}}{14}{159} \secentry {Command Line Options}{14}{1}{159} \secentry {Other Command Line Arguments}{14}{2}{163} \secentry {The \code {AWKPATH} Environment Variable}{14}{3}{164} \secentry {Obsolete Options and/or Features}{14}{4}{165} \secentry {Undocumented Options and Features}{14}{5}{165} \secentry {Known Bugs in \code {gawk}}{14}{6}{165} \chapentry {A Library of \code {awk} Functions}{15}{167} \secentry {Simulating \code {gawk}-specific Features}{15}{1}{167} \secentry {Implementing \code {nextfile} as a Function}{15}{2}{168} \secentry {Assertions}{15}{3}{170} \secentry {Rounding Numbers}{15}{4}{171} \secentry {Translating Between Characters and Numbers}{15}{5}{172} \secentry {Merging an Array Into a String}{15}{6}{174} \secentry {Turning Dates Into Timestamps}{15}{7}{175} \secentry {Managing the Time of Day}{15}{8}{181} \secentry {Noting Data File Boundaries}{15}{9}{183} \secentry {Processing Command Line Options}{15}{10}{184} \secentry {Reading the User Database}{15}{11}{190} \secentry {Reading the Group Database}{15}{12}{195} \secentry {Naming Library Function Global Variables}{15}{13}{200} \chapentry {Practical \code {awk} Programs}{16}{203} \secentry {Re-inventing Wheels for Fun and Profit}{16}{1}{203} \subsecentry {Cutting Out Fields and Columns}{16}{1}{1}{203} \subsecentry {Searching for Regular Expressions in Files}{16}{1}{2}{208} \subsecentry {Printing Out User Information}{16}{1}{3}{213} \subsecentry {Splitting a Large File Into Pieces}{16}{1}{4}{215} \subsecentry {Duplicating Output Into Multiple Files}{16}{1}{5}{217} \subsecentry {Printing Non-duplicated Lines of Text}{16}{1}{6}{218} \subsecentry {Counting Things}{16}{1}{7}{223} \secentry {A Grab Bag of \code {awk} Programs}{16}{2}{225} \subsecentry {Finding Duplicated Words in a Document}{16}{2}{1}{226} \subsecentry {An Alarm Clock Program}{16}{2}{2}{226} \subsecentry {Transliterating Characters}{16}{2}{3}{229} \subsecentry {Printing Mailing Labels}{16}{2}{4}{231} \subsecentry {Generating Word Usage Counts}{16}{2}{5}{233} \subsecentry {Removing Duplicates from Unsorted Text}{16}{2}{6}{236} \subsecentry {Extracting Programs from Texinfo Source Files}{16}{2}{7}{236} \subsecentry {A Simple Stream Editor}{16}{2}{8}{240} \subsecentry {An Easy Way to Use Library Functions}{16}{2}{9}{242} \chapentry {The Evolution of the \code {awk} Language}{17}{251} \secentry {Major Changes between V7 and SVR3.1}{17}{1}{251} \secentry {Changes between SVR3.1 and SVR4}{17}{2}{252} \secentry {Changes between SVR4 and POSIX \code {awk}}{17}{3}{253} \secentry {Extensions in the Bell Laboratories \code {awk}}{17}{4}{253} \secentry {Extensions in \code {gawk} Not in POSIX \code {awk}}{17}{5}{254} \chapentry {\code {gawk} Summary}{Appendix{} \char65}{257} \secentry {Command Line Options Summary}{\char65}{1}{257} \secentry {Language Summary}{\char65}{2}{259} \secentry {Variables and Fields}{\char65}{3}{259} \subsecentry {Fields}{\char65}{3}{1}{260} \subsecentry {Built-in Variables}{\char65}{3}{2}{260} \subsecentry {Arrays}{\char65}{3}{3}{262} \subsecentry {Data Types}{\char65}{3}{4}{262} \secentry {Patterns}{\char65}{4}{263} \subsecentry {Pattern Summary}{\char65}{4}{1}{264} \subsecentry {Regular Expressions}{\char65}{4}{2}{265} \secentry {Actions}{\char65}{5}{266} \subsecentry {Operators}{\char65}{5}{1}{267} \subsecentry {Control Statements}{\char65}{5}{2}{268} \subsecentry {I/O Statements}{\char65}{5}{3}{268} \subsecentry {\code {printf} Summary}{\char65}{5}{4}{269} \subsecentry {Special File Names}{\char65}{5}{5}{271} \subsecentry {Built-in Functions}{\char65}{5}{6}{272} \subsecentry {Time Functions}{\char65}{5}{7}{274} \subsecentry {String Constants}{\char65}{5}{8}{274} \secentry {User-defined Functions}{\char65}{6}{275} \secentry {Historical Features}{\char65}{7}{275} \chapentry {Installing \code {gawk}}{Appendix{} \char66}{277} \secentry {The \code {gawk} Distribution}{\char66}{1}{277} \subsecentry {Getting the \code {gawk} Distribution}{\char66}{1}{1}{277} \subsecentry {Extracting the Distribution}{\char66}{1}{2}{279} \subsecentry {Contents of the \code {gawk} Distribution}{\char66}{1}{3}{279} \secentry {Compiling and Installing \code {gawk} on Unix}{\char66}{2}{282} \subsecentry {Compiling \code {gawk} for Unix}{\char66}{2}{1}{282} \subsecentry {The Configuration Process}{\char66}{2}{2}{283} \secentry {How to Compile and Install \code {gawk} on VMS}{\char66}{3}{283} \subsecentry {Compiling \code {gawk} on VMS}{\char66}{3}{1}{283} \subsecentry {Installing \code {gawk} on VMS}{\char66}{3}{2}{284} \subsecentry {Running \code {gawk} on VMS}{\char66}{3}{3}{285} \subsecentry {Building and Using \code {gawk} on VMS POSIX}{\char66}{3}{4}{285} \secentry {MS-DOS and OS/2 Installation and Compilation}{\char66}{4}{286} \secentry {Installing \code {gawk} on the Atari ST}{\char66}{5}{287} \subsecentry {Compiling \code {gawk} on the Atari ST}{\char66}{5}{1}{287} \subsecentry {Running \code {gawk} on the Atari ST}{\char66}{5}{2}{288} \secentry {Installing \code {gawk} on an Amiga}{\char66}{6}{289} \secentry {Reporting Problems and Bugs}{\char66}{7}{290} \secentry {Other Freely Available \code {awk} Implementations}{\char66}{8}{291} \chapentry {Implementation Notes}{Appendix{} \char67}{293} \secentry {Downward Compatibility and Debugging}{\char67}{1}{293} \secentry {Making Additions to \code {gawk}}{\char67}{2}{293} \subsecentry {Adding New Features}{\char67}{2}{1}{293} \subsecentry {Porting \code {gawk} to a New Operating System}{\char67}{2}{2}{295} \secentry {Probable Future Extensions}{\char67}{3}{297} \secentry {Suggestions for Improvements}{\char67}{4}{298} \chapentry {Glossary}{Appendix{} \char68}{299} \unnumbchapentry {GNU GENERAL PUBLIC LICENSE}{307} \unnumbsecentry{Preamble}{307} \unnumbsecentry{TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION}{308} \unnumbsecentry{How to Apply These Terms to Your New Programs}{313} \unnumbchapentry {Index}{315}