GNU Awk - Munier.perso.univ-pau.fr

1y ago
13 Views
2 Downloads
2.01 MB
560 Pages
Last View : 1m ago
Last Download : 2m ago
Upload by : Giovanna Wyche
Transcription

GNU Awk

GAWK: Effective AWK ProgrammingA User’s Guide for GNU AwkEdition 4.2October, 2017Arnold D. Robbins

“To boldly go where no man has gone before” is a Registered Trademark of ParamountPictures Corporation.Published by:Free Software Foundation51 Franklin Street, Fifth FloorBoston, MA 02110-1301 USAPhone: 1-617-542-5942Fax: 1-617-542-2652Email: gnu@gnu.orgURL: https://www.gnu.org/ISBN 1-882114-28-0Copyright c 1989, 1991, 1992, 1993, 1996–2005, 2007, 2009–2017Free Software Foundation, Inc.This is Edition 4.2 of GAWK: Effective AWK Programming: A User’s Guide for GNU Awk,for the 4.2.0 (or later) version of the GNU implementation of AWK.Permission is granted to copy, distribute and/or modify this document under the termsof the GNU Free Documentation License, Version 1.3 or any later version published bythe Free Software Foundation; with the Invariant Sections being “GNU General PublicLicense”, with the Front-Cover Texts being “A GNU Manual”, and with the Back-CoverTexts as in (a) below. A copy of the license is included in the section entitled “GNU FreeDocumentation License”.a. The FSF’s Back-Cover Text is: “You have the freedom to copy and modify this GNUmanual.”

To my parents, for their love, and for the wonderful example they set for me.To my wife, Miriam, for making me complete.Thank you for building your life together with me.To our children, Chana, Rivka, Nachum, and Malka, for enrichening our lives in innumerable ways.

iShort ContentsForeword to the Third Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Foreword to the Fourth Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Part I: The awk Language1Getting Started with awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1723Running awk and gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474Reading Input Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615Printing Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9367Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113Patterns, Actions, and Variables . . . . . . . . . . . . . . . . . . . . . . . . 1418Arrays in awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1699Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185Part II: Problem Solving with awk10 A Library of awk Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23111 Practical awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265Part III: Moving Beyond Standard awk with gawk12 Advanced Features of gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31313 Internationalization with gawk . . . . . . . . . . . . . . . . . . . . . . . . . 32914 Debugging awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33915 Arithmetic and Arbitrary-Precision Arithmetic with gawk . . . 35516 Writing Extensions for gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . 369Part IV: AppendicesAThe Evolution of the awk Language . . . . . . . . . . . . . . . . . . . . . 433BInstalling gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451CDImplementation Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469Basic Programming Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . 481Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485GNU General Public License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497GNU Free Documentation License . . . . . . . . . . . . . . . . . . . . . . . . . . 509Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517

iiiTable of ContentsForeword to the Third Edition . . . . . . . . . . . . . . . . . . . . . 1Foreword to the Fourth Edition . . . . . . . . . . . . . . . . . . . . 3Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5History of awk and gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6A Rose by Any Other Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Using This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Typographical Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Dark Corners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10The GNU Project and This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10How to Contribute. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Part I: The awk Language1Getting Started with awk . . . . . . . . . . . . . . . . . . . . . . 171.1How to Run awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171.1.1 One-Shot Throwaway awk Programs . . . . . . . . . . . . . . . . . . . . . . 171.1.2 Running awk Without Input Files . . . . . . . . . . . . . . . . . . . . . . . . . 181.1.3 Running Long Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181.1.4 Executable awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191.1.5 Comments in awk Programs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201.1.6 Shell Quoting Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211.1.6.1 Quoting in MS-Windows Batch Files . . . . . . . . . . . . . . . . . 231.2 Data files for the Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241.3 Some Simple Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251.4 An Example with Two Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261.5 A More Complex Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271.6 awk Statements Versus Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281.7 Other Features of awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291.8 When to Use awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301.9 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302Running awk and gawk . . . . . . . . . . . . . . . . . . . . . . . . . . 312.12.22.32.42.5Invoking awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Command-Line Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Other Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38Naming Standard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39The Environment Variables gawk Uses . . . . . . . . . . . . . . . . . . . . . . . . . 392.5.1 The AWKPATH Environment Variable . . . . . . . . . . . . . . . . . . . . . . . 39

iv GAWK: Effective AWK Programming2.5.2 The AWKLIBPATH Environment Variable . . . . . . . . . . . . . . . . . . . 402.5.3 Other Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402.6 gawk’s Exit Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422.7 Including Other Files into Your Program. . . . . . . . . . . . . . . . . . . . . . . 422.8 Loading Dynamic Extensions into Your Program . . . . . . . . . . . . . . . 432.9 Obsolete Options and/or Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442.10 Undocumented Options and Features . . . . . . . . . . . . . . . . . . . . . . . . . 442.11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 473.1 How to Use Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473.2 Escape Sequences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483.3 Regular Expression Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503.4 Using Bracket Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533.5 How Much Text Matches? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553.6 Using Dynamic Regexps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553.7 gawk-Specific Regexp Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563.8 Case Sensitivity in Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583.9 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594Reading Input Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614.1How Input Is Split into Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614.1.1 Record Splitting with Standard awk . . . . . . . . . . . . . . . . . . . . . . . 614.1.2 Record Splitting with gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634.2 Examining Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654.3 Nonconstant Field Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664.4 Changing the Contents of a Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674.5 Specifying How Fields Are Separated . . . . . . . . . . . . . . . . . . . . . . . . . . 694.5.1 Whitespace Normally Separates Fields . . . . . . . . . . . . . . . . . . . . 704.5.2 Using Regular Expressions to Separate Fields . . . . . . . . . . . . . 704.5.3 Making Each Character a Separate Field . . . . . . . . . . . . . . . . . . 714.5.4 Setting FS from the Command Line . . . . . . . . . . . . . . . . . . . . . . . 714.5.5 Making the Full Line Be a Single Field . . . . . . . . . . . . . . . . . . . . 734.5.6 Field-Splitting Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734.6 Reading Fixed-Width Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744.6.1 Processing Fixed-Width Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744.6.2 Skipping Intervening Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764.6.3 Capturing Optional Trailing Data . . . . . . . . . . . . . . . . . . . . . . . . . 764.6.4 Field Values With Fixed-Width Data . . . . . . . . . . . . . . . . . . . . . 764.7 Defining Fields by Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774.8 Checking How gawk Is Splitting Records . . . . . . . . . . . . . . . . . . . . . . . 784.9 Multiple-Line Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794.10 Explicit Input with getline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814.10.1 Using getline with No Arguments . . . . . . . . . . . . . . . . . . . . . . 824.10.2 Using getline into a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . 834.10.3 Using getline from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834.10.4 Using getline into a Variable from a File . . . . . . . . . . . . . . . 84

v4.10.5 Using getline from a Pipe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844.10.6 Using getline into a Variable from a Pipe . . . . . . . . . . . . . . . 864.10.7 Using getline from a Coprocess . . . . . . . . . . . . . . . . . . . . . . . . . 864.10.8 Using getline into a Variable from a Coprocess . . . . . . . . . 864.10.9 Points to Remember About getline . . . . . . . . . . . . . . . . . . . . . 864.10.10 Summary of getline Variants . . . . . . . . . . . . . . . . . . . . . . . . . . 874.11 Reading Input with a Timeout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 884.12 Retrying Reads After Certain Input Errors . . . . . . . . . . . . . . . . . . . 894.13 Directories on the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . 904.14 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 904.15 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915Printing Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 935.1 The print Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 935.2 print Statement Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 935.3 Output Separators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955.4 Controlling Numeric Output with print . . . . . . . . . . . . . . . . . . . . . . . 965.5 Using printf Statements for Fancier Printing . . . . . . . . . . . . . . . . . . 965.5.1 Introduction to the printf Statement. . . . . . . . . . . . . . . . . . . . . 965.5.2 Format-Control Letters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975.5.3 Modifiers for printf Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985.5.4 Examples Using printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1005.6 Redirecting Output of print and printf . . . . . . . . . . . . . . . . . . . . . 1015.7 Special Files for Standard Preopened Data Streams . . . . . . . . . . . 1045.8 Special File names in gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1055.8.1 Accessing Other Open Files with gawk . . . . . . . . . . . . . . . . . . . 1055.8.2 Special Files for Network Communications . . . . . . . . . . . . . . . 1055.8.3 Special File name Caveats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1065.9 Closing Input and Output Redirections . . . . . . . . . . . . . . . . . . . . . . . 1065.10 Enabling Nonfatal Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1095.11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1105.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1106Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1136.1Constants, Variables, and Conversions . . . . . . . . . . . . . . . . . . . . . . . . 1136.1.1 Constant Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1136.1.1.1 Numeric and String Constants . . . . . . . . . . . . . . . . . . . . . . 1136.1.1.2 Octal and Hexadecimal Numbers . . . . . . . . . . . . . . . . . . . . 1136.1.1.3 Regular Expression Constants. . . . . . . . . . . . . . . . . . . . . . . 1146.1.2 Using Regular Expression Constants . . . . . . . . . . . . . . . . . . . . . 1156.1.2.1 Standard Regular Expression Constants . . . . . . . . . . . . . 1156.1.2.2 Strongly Typed Regexp Constants . . . . . . . . . . . . . . . . . . 1166.1.3 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1176.1.3.1 Using Variables in a Program . . . . . . . . . . . . . . . . . . . . . . . 1176.1.3.2 Assigning Variables on the Command Line . . . . . . . . . . 1186.1.4 Conversion of Strings and Numbers . . . . . . . . . . . . . . . . . . . . . . 1186.1.4.1 How awk Converts Between Strings and Numbers . . . 118

vi GAWK: Effective AWK Programming6.1.4.2 Locales Can Influence Conversion . . . . . . . . . . . . . . . . . . . 119Operators: Doing Something with Values . . . . . . . . . . . . . . . . . . . . . 1216.2.1 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1216.2.2 String Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1226.2.3 Assignment Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1236.2.4 Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . 1266.3 Truth Values and Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1276.3.1 True and False in awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1276.3.2 Variable Typing and Comparison Expressions . . . . . . . . . . . . 1276.3.2.1 String Type versus Numeric Type . . . . . . . . . . . . . . . . . . . 1286.3.2.2 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1306.3.2.3 String Comparison Based on Locale Collating Order . . 1326.3.3 Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1326.3.4 Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1346.4 Function Calls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1356.5 Operator Precedence (How Operators Nest). . . . . . . . . . . . . . . . . . . 1366.6 Where You Are Makes a Difference . . . . . . . . . . . . . . . . . . . . . . . . . . . 1376.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1386.27Patterns, Actions, and Variables . . . . . . . . . . . . 1417.1Pattern Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1417.1.1 Regular Expressions as Patterns . . . . . . . . . . . . . . . . . . . . . . . . . 1417.1.2 Expressions as Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1417.1.3 Specifying Record Ranges with Patterns . . . . . . . . . . . . . . . . . 1437.1.4 The BEGIN and END Special Patterns . . . . . . . . . . . . . . . . . . . . . 1447.1.4.1 Startup and Cleanup Actions . . . . . . . . . . . . . . . . . . . . . . . 1447.1.4.2 Input/Output from BEGIN and END Rules. . . . . . . . . . . . 1457.1.5 The BEGINFILE and ENDFILE Special Patterns. . . . . . . . . . . . 1457.1.6 The Empty Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1467.2 Using Shell Variables in Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1467.3 Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1477.4 Control Statements in Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1487.4.1 The if-else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1487.4.2 The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1497.4.3 The do-while Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1507.4.4 The for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1507.4.5 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1517.4.6 The break Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1527.4.7 The continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1537.4.8 The next Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1547.4.9 The nextfile Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1557.4.10 The exit Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1567.5 Predefined Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1577.5.1 Built-in Variables That Control awk. . . . . . . . . . . . . . . . . . . . . . 1577.5.2 Built-in Variables That Convey Information . . . . . . . . . . . . . . 1597.5.3 Using ARGC and ARGV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1667.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

vii8Arrays in awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1698.1The Basics of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1698.1.1 Introduction to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1698.1.2 Referring to an Array Element . . . . . . . . . . . . . . . . . . . . . . . . . . . 1718.1.3 Assigning Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1728.1.4 Basic Array Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1728.1.5 Scanning All Elements of an Array. . . . . . . . . . . . . . . . . . . . . . . 1738.1.6 Using Predefined Array Scanning Orders with gawk . . . . . . 1748.2 Using Numbers to Subscript Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . 1778.3 Using Uninitialized Variables as Subscripts. . . . . . . . . . . . . . . . . . . . 1778.4 The delete Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1788.5 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1798.5.1 Scanning Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . 1818.6 Arrays of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1818.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1839Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1859.1Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1859.1.1 Calling Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1859.1.2 Numeric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1869.1.3 String-Manipulation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 1879.1.3.1 More about ‘\’ and ‘&’ withsub(), gsub(), and gensub() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1969.1.4 Input/Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1999.1.5 Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2029.1.6 Bit-Manipulation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2079.1.7 Getting Type Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2109.1.8 String-Translation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2119.2 User-Defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2119.2.1 Function Definition Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2119.2.2 Function Definition Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2139.2.3 Calling User-Defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 2159.2.3.1 Writing a Function Call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2159.2.3.2 Controlling Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . 2159.2.3.3 Passing Function Arguments by Value Or by Reference . . 2179.2.4 The return Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2199.2.5 Functions and Their Effects on Variable Typing . . . . . . . . . . 2209.3 Indirect Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2219.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

viii GAWK: Effective AWK ProgrammingPart II: Problem Solving with awk10A Library of awk Functions . . . . . . . . . . . . . . . . . 23110.1 Naming Library Function Global Variables . . . . . . . . . . . . . . . . . . 23210.2 General Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23310.2.1 Converting Strings to Numbers . . . . . . . . . . . . . . . . . . . . . . . . . 23310.2.2 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23410.2.3 Rounding Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23610.2.4 The Cliff Random Number Generator . . . . . . . . . . . . . . . . . . . 23710.2.5 Translating Between Characters and Numbers . . . . . . . . . . 23710.2.6 Merging an Array into a String . . . . . . . . . . . . . . . . . . . . . . . . . 23910.2.7 Managing the Time of Day . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23910.2.8 Reading a Whole File at Once . . . . . . . . . . . . . . . . . . . . . . . . . . 24110.2.9 Quoting Strings to Pass to the Shell . . . . . . . . . . . . . . . . . . . . 24210.3 Data file Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24310.3.1 Noting Data file Boundaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24310.3.2 Rereading the Current File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24410.3.3 Checking for Readable Data files. . . . . . . . . . . . . . . . . . . . . . . . 24610.3.4 Checking for Zero-Length Files . . . . . . . . . . . . . . . . . . . . . . . . . 24610.3.5 Treating Assignments as File names . . . . . . . . . . . . . . . . . . . . 24710.4 Processing Command-Line Options . . . . . . . . . . . . . . . . . . . . . . . . . . 24810.5 Reading the User Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25310.6 Reading the Group Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25710.7 Traversing Arrays of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26110.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26310.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26311Practical awk Programs. . . . . . . . . . . . . . . . . . . . . . 26511.1 Running the Example Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26511.2 Reinventing Wheels for Fun and Profit . . . . . . . . . . . . . . . . . . . . . . 26511.2.1 Cutting Out Fields and Columns . . . . . . . . . . . . . . . . . . . . . . . 26511.2.2 Searching for Regular Expressions in Files . . . . . . . . . . . . . . 27011.2.3 Printing Out User Information. . . . . . . . . . . . . . . . . . . . . . . . . . 27411.2.4 Splitting a Large File into Pieces . . . . . . . . . . . . . . . . . . . . . . . 27611.2.5 Duplicating Output into Multiple Files . . . . . . . . . . . . . . . . . 27711.2.6 Printing Nonduplicated Lines of Text . . . . . . . . . . . . . . . . . . . 27911.2.7 Counting Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28311.3 A Grab Bag of awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28511.3.1 Finding Duplicated Words in a Document . . . . . . . . . . . . . . 28511.3.2 An Alarm Clock Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28611.3.3 Transliterating Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28811.3.4 Printing Mailing Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29011.3.5 Generating Word-Usage Counts. . . . . . . . . . . . . . . . . . . . . . . . . 29211.3.6 Removing Duplicates from Unsorted Text . . . . . . . . . . . . . . . 29411.3.7 Extracting Programs from Texinfo Source Files . . . . . . . . . 29511.3.8 A Simple Stream Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29811.3.9 An Easy Way to Use Library Functions . . . . . . . . . . . . . . . . . 299

ix11.3.10 Finding Anagrams from a Dictionary . . . . . . . . . . . . . . . . . . 30611.3.11 And Now for Something Completely Different . . . . . . . . . . 30711.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30811.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308Part III: Moving Beyond Standard awk with gawk12Advanced Features of gawk . . . . . . . . . . . . . . . . . . 31312.1 Allowing Nondecimal Input Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31312.2 Controlling Array Traversal and Array Sorting . . . . . . . . . . . . . . . 31412.2.1 Controlling Array Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31412.2.2 Sorting Array Values and Indices with gawk . . . . . . . . . . . . . 31812.3 Two-Way Communications with Another Process . . . . . . . . . . . . 32012.4 Using gawk for Network Programming . . . . . . . . . . . . . . . . . . . . . . . 32312.5 Profiling Your awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32412.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32813Internationalization with gawk . . . . . . . . . . . . . . 32913.1 Internationalization and Localization . . . . . . . . . . . . . . . . . . . . . . . . 32913.2 GNU gettext. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32913.3 Internationalizing awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33113.4 Translating awk Programs . . . . . . . . .

This is Edition 4.2 of GAWK: Effective AWK Programming: A User's Guide for GNU Awk, for the 4.2.0 (or later) version of the GNU implementation of AWK. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by

Related Documents:

-This makes awk one of the most powerful of the Unix utilities awk processes fields while sed only processes lines nawk (new awk) is the new standard for awk -Designed to facilitate large awk programs -gawk is a free nawk clone from GNU awk gets it's input from -files -redirection and pipes -directly from standard input

This is Edition 4.1 of GAWK: Effective AWK Programming: A User's Guide for GNU Awk, for the 4.1.4 (or later) version of the GNU implementation of AWK. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by

This is Edition 4.1 of GAWK: Effective AWK Programming: A User's Guide for GNU Awk, for the 4.1.1 (or later) version of the GNU implementation of AWK. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the

This is Edition 3 of GAWK: Effective AWK Programming: A User's Guide for GNU Awk, for the 3.1.4 (or later) version of the GNU implementation of AWK. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the

This is Edition 5.0 of GAWK: Effective AWK Programming: A User's Guide for GNU Awk, for the 5.0.0 (or later) version of the GNU implementation of AWK. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by

This is Edition 5.1 of GAWK: Effective AWK Programming: A User’s Guide for GNU Awk, for the 5.1.0 (or later) version of the GNU implementation of AWK. Permission is granted to cop

This is Edition 4.1 of GAWK: Effective AWK Programming: A User's Guide for GNU Awk, for the 4.1.0 (or later) version of the GNU implementation of AWK. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the

Following the publication of the UK Sanctions List, information on the Consolidated List has been updated. Notice summary 4. The following entries have been amended and are still subject to an asset freeze: Ntabo Ntaberi SHEKA (Group ID: 12438). Bosco TAGANDA (Group ID: 8736). 2 What you must do 5. You must: i. check whether you maintain any accounts or hold any funds or economic .