GNU Awk - Docs.linux-code

1y ago
10 Views
2 Downloads
2.06 MB
570 Pages
Last View : 1m ago
Last Download : 2m ago
Upload by : Ronan Garica
Transcription

GNU Awk

GAWK: Effective AWK ProgrammingA User’s Guide for GNU AwkEdition 5.0March, 2019Arnold 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–2019Free Software Foundation, Inc.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 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1719Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187Part II: Problem Solving with awk10 A Library of awk Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23311 Practical awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267Part III: Moving Beyond Standard awk with gawk12 Advanced Features of gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31513 Internationalization with gawk . . . . . . . . . . . . . . . . . . . . . . . . . 33314 Debugging awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34315 Namespaces in gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35916 Arithmetic and Arbitrary-Precision Arithmetic with gawk . . . 36517 Writing Extensions for gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . 379Part IV: AppendicesAThe Evolution of the awk Language . . . . . . . . . . . . . . . . . . . . . 445BCInstalling gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463Implementation Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481DBasic Programming Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . 491Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495GNU General Public License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507GNU Free Documentation License . . . . . . . . . . . . . . . . . . . . . . . . . . 519

ii GAWK: Effective AWK ProgrammingIndex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527

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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231.3 Some Simple Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241.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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412.6 gawk’s Exit Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422.7 Including Other Files into Your Program. . . . . . . . . . . . . . . . . . . . . . . 422.8 Loading Dynamic Extensions into Your Program . . . . . . . . . . . . . . . 442.9 Obsolete Options and/or Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442.10 Undocumented Options and Features . . . . . . . . . . . . . . . . . . . . . . . . . 452.11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 473.13.23.3How to Use Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47Escape Sequences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48Regular Expression Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503.3.1 Regexp Operators in awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503.3.2 Some Notes On Interval Expressions . . . . . . . . . . . . . . . . . . . . . . 523.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 . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

v4.10.3 Using getline from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834.10.4 Using getline into a Variable from a File . . . . . . . . . . . . . . . 844.10.5 Using getline from a Pipe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854.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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 995.5.4 Examples Using printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1015.6 Redirecting Output of print and printf . . . . . . . . . . . . . . . . . . . . . 1025.7 Special Files for Standard Preopened Data Streams . . . . . . . . . . . 1045.8 Special File names in gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1055.8.1 Accessing Other Open Files with gawk . . . . . . . . . . . . . . . . . . . 1065.8.2 Special Files for Network Communications . . . . . . . . . . . . . . . 1065.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 . . . . . . . . . . . . . . . . . . . . 1146.1.1.3 Regular Expression Constants. . . . . . . . . . . . . . . . . . . . . . . 1156.1.2 Using Regular Expression Constants . . . . . . . . . . . . . . . . . . . . . 1156.1.2.1 Standard Regular Expression Constants . . . . . . . . . . . . . 1166.1.2.2 Strongly Typed Regexp Constants . . . . . . . . . . . . . . . . . . 1176.1.3 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1186.1.3.1 Using Variables in a Program . . . . . . . . . . . . . . . . . . . . . . . 1186.1.3.2 Assigning Variables on the Command Line . . . . . . . . . . 118

vi GAWK: Effective AWK Programming6.1.4 Conversion of Strings and Numbers . . . . . . . . . . . . . . . . . . . . . . 1196.1.4.1 How awk Converts Between Strings and Numbers . . . 1196.1.4.2 Locales Can Influence Conversion . . . . . . . . . . . . . . . . . . . 1206.2 Operators: Doing Something with Values . . . . . . . . . . . . . . . . . . . . . 1226.2.1 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1226.2.2 String Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1236.2.3 Assignment Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1246.2.4 Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . 1276.3 Truth Values and Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1286.3.1 True and False in awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1286.3.2 Variable Typing and Comparison Expressions . . . . . . . . . . . . 1286.3.2.1 String Type versus Numeric Type . . . . . . . . . . . . . . . . . . . 1296.3.2.2 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1316.3.2.3 String Comparison Based on Locale Collating Order . . 1336.3.3 Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1336.3.4 Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1356.4 Function Calls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1366.5 Operator Precedence (How Operators Nest). . . . . . . . . . . . . . . . . . . 1376.6 Where You Are Makes a Difference . . . . . . . . . . . . . . . . . . . . . . . . . . . 1386.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1397Patterns, 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

vii7.68Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168Arrays in awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1718.1The Basics of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1718.1.1 Introduction to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1718.1.2 Referring to an Array Element . . . . . . . . . . . . . . . . . . . . . . . . . . . 1738.1.3 Assigning Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1748.1.4 Basic Array Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1748.1.5 Scanning All Elements of an Array. . . . . . . . . . . . . . . . . . . . . . . 1758.1.6 Using Predefined Array Scanning Orders with gawk . . . . . . 1768.2 Using Numbers to Subscript Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . 1798.3 Using Uninitialized Variables as Subscripts. . . . . . . . . . . . . . . . . . . . 1798.4 The delete Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1808.5 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1818.5.1 Scanning Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . 1838.6 Arrays of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1838.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1859Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1879.1Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1879.1.1 Calling Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1879.1.2 Numeric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1889.1.3 String-Manipulation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 1899.1.3.1 More about ‘\’ and ‘&’ withsub(), gsub(), and gensub() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1989.1.4 Input/Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2019.1.5 Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2059.1.6 Bit-Manipulation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2109.1.7 Getting Type Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2139.1.8 String-Translation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2149.2 User-Defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2149.2.1 Function Definition Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2149.2.2 Function Definition Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2169.2.3 Calling User-Defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 2189.2.3.1 Writing a Function Call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2189.2.3.2 Controlling Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . 2189.2.3.3 Passing Function Arguments by Value Or by Reference . . 2209.2.3.4 Other Points About Calling Functions . . . . . . . . . . . . . . 2219.2.4 The return Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2229.2.5 Functions and Their Effects on Variable Typing . . . . . . . . . . 2249.3 Indirect Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2249.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229Part II: Problem Solving with awk

viii GAWK: Effective AWK Programming10A Library of awk Functions . . . . . . . . . . . . . . . . . 23310.1 Naming Library Function Global Variables . . . . . . . . . . . . . . . . . . 23410.2 General Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23510.2.1 Converting Strings to Numbers . . . . . . . . . . . . . . . . . . . . . . . . . 23510.2.2 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23610.2.3 Rounding Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23810.2.4 The Cliff Random Number Generator . . . . . . . . . . . . . . . . . . . 23910.2.5 Translating Between Characters and Numbers . . . . . . . . . . 23910.2.6 Merging an Array into a String . . . . . . . . . . . . . . . . . . . . . . . . . 24110.2.7 Managing the Time of Day . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24110.2.8 Reading a Whole File at Once . . . . . . . . . . . . . . . . . . . . . . . . . . 24310.2.9 Quoting Strings to Pass to the Shell . . . . . . . . . . . . . . . . . . . . 24410.3 Data file Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24510.3.1 Noting Data file Boundaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24510.3.2 Rereading the Current File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24610.3.3 Checking for Readable Data files. . . . . . . . . . . . . . . . . . . . . . . . 24810.3.4 Checking for Zero-Length Files . . . . . . . . . . . . . . . . . . . . . . . . . 24810.3.5 Treating Assignments as File names . . . . . . . . . . . . . . . . . . . . 24910.4 Processing Command-Line Options . . . . . . . . . . . . . . . . . . . . . . . . . . 25010.5 Reading the User Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25510.6 Reading the Group Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25910.7 Traversing Arrays of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26310.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26510.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26511Practical awk Programs. . . . . . . . . . . . . . . . . . . . . . 26711.1 Running the Example Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26711.2 Reinventing Wheels for Fun and Profit . . . . . . . . . . . . . . . . . . . . . . 26711.2.1 Cutting Out Fields and Columns . . . . . . . . . . . . . . . . . . . . . . . 26711.2.2 Searching for Regular Expressions in Files . . . . . . . . . . . . . . 27211.2.3 Printing Out User Information. . . . . . . . . . . . . . . . . . . . . . . . . . 27611.2.4 Splitting a Large File into Pieces . . . . . . . . . . . . . . . . . . . . . . . 27811.2.5 Duplicating Output into Multiple Files . . . . . . . . . . . . . . . . . 27911.2.6 Printing Nonduplicated Lines of Text . . . . . . . . . . . . . . . . . . . 28111.2.7 Counting Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28511.3 A Grab Bag of awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28711.3.1 Finding Duplicated Words in a Document . . . . . . . . . . . . . . 28711.3.2 An Alarm Clock Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28811.3.3 Transliterating Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29011.3.4 Printing Mailing Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29311.3.5 Generating Word-Usage Counts. . . . . . . . . . . . . . . . . . . . . . . . . 29411.3.6 Removing Duplicates from Unsorted Text . . . . . . . . . . . . . . . 29611.3.7 Extracting Programs from Texinfo Source Files . . . . . . . . . 29711.3.8 A Simple Stream Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30011.3.9 An Easy Way to Use Library Functions . . . . . . . . . . . . . . . . . 30211.3.10 Finding Anagrams from a Dictionary . . . . . . . . . . . . . . . . . . 30811.3.11 And Now for Something Completely Different . . . . . . . . . . 31011.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

ix11.5Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310Part III: Moving Beyond Standard awk with gawk12Advanced Features of gawk . . . . . . . . . . . . . . . . . . 31512.1 Allowing Nondecimal Input Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31512.2 Controlling Array Traversal and Array Sorting . . . . . . . . . . . . . . . 31612.2.1 Controlling Array Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31612.2.2 Sorting Array Values and Indices with gawk . . . . . . . . . . . . . 32012.3 Two-Way Communications with Another Process . . . . . . . . . . . . 32212.4 Using gawk for Network Programming . . . . . . . . . . . . . . . . . . . . . . . 32512.5 Profiling Your awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32712.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33113Internationalization with ga

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

Related Documents:

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 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 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

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

Korean language training. In recent decades the number of KFL textbooks for English-speaking KFL classroom use has steadily increased. However, the number of KFL study materials intended for a self-study purpose is still relatively scarce. Furthermore, to date there has been no published KFL grammar workbook that specifically aims at providing supplemental grammar explanations and exercises in .