GNU Awk - Clemson University

1y ago
12 Views
2 Downloads
1.95 MB
540 Pages
Last View : 1m ago
Last Download : 2m ago
Upload by : Randy Pettway
Transcription

GNU Awk

GAWK: Effective AWK ProgrammingA User’s Guide for GNU AwkEdition 4.1August, 2016Arnold 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: http://www.gnu.org/ISBN 1-882114-28-0Copyright c 1989, 1991, 1992, 1993, 1996–2005, 2007, 2009–2016Free Software Foundation, Inc.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 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454Reading Input Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595Printing Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8967Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107Patterns, Actions, and Variables . . . . . . . . . . . . . . . . . . . . . . . . 1318Arrays in awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1599Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175Part II: Problem Solving with awk10 A Library of awk Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21911 Practical awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253Part III: Moving Beyond Standard awk with gawk12 Advanced Features of gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30113 Internationalization with gawk . . . . . . . . . . . . . . . . . . . . . . . . . 31714 Debugging awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32715 Arithmetic and Arbitrary-Precision Arithmetic with gawk . . . 34316 Writing Extensions for gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . 357Part IV: AppendicesAThe Evolution of the awk Language . . . . . . . . . . . . . . . . . . . . . 415BInstalling gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431CDImplementation Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451Basic Programming Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . 463Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467GNU General Public License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479GNU Free Documentation License . . . . . . . . . . . . . . . . . . . . . . . . . . 491Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499

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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261.6 awk Statements Versus Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271.7 Other Features of awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291.8 When to Use awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291.9 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302Running awk and gawk . . . . . . . . . . . . . . . . . . . . . . . . . . 312.12.22.32.42.5Invoking awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Command-Line Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Other Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Naming Standard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38The 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412.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 . . . . . . . . . . . . . . . . . . . . . . . . . . . 453.1 How to Use Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453.2 Escape Sequences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463.3 Regular Expression Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483.4 Using Bracket Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503.5 How Much Text Matches? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523.6 Using Dynamic Regexps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533.7 gawk-Specific Regexp Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543.8 Case Sensitivity in Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553.9 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564Reading Input Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594.1How Input Is Split into Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594.1.1 Record Splitting with Standard awk . . . . . . . . . . . . . . . . . . . . . . . 594.1.2 Record Splitting with gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614.2 Examining Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634.3 Nonconstant Field Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644.4 Changing the Contents of a Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654.5 Specifying How Fields Are Separated . . . . . . . . . . . . . . . . . . . . . . . . . . 674.5.1 Whitespace Normally Separates Fields . . . . . . . . . . . . . . . . . . . . 684.5.2 Using Regular Expressions to Separate Fields . . . . . . . . . . . . . 684.5.3 Making Each Character a Separate Field . . . . . . . . . . . . . . . . . . 694.5.4 Setting FS from the Command Line . . . . . . . . . . . . . . . . . . . . . . . 694.5.5 Making the Full Line Be a Single Field . . . . . . . . . . . . . . . . . . . . 714.5.6 Field-Splitting Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714.6 Reading Fixed-Width Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724.7 Defining Fields by Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744.8 Multiple-Line Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764.9 Explicit Input with getline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784.9.1 Using getline with No Arguments . . . . . . . . . . . . . . . . . . . . . . . 784.9.2 Using getline into a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794.9.3 Using getline from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804.9.4 Using getline into a Variable from a File. . . . . . . . . . . . . . . . . 804.9.5 Using getline from a Pipe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814.9.6 Using getline into a Variable from a Pipe . . . . . . . . . . . . . . . . 824.9.7 Using getline from a Coprocess . . . . . . . . . . . . . . . . . . . . . . . . . . 824.9.8 Using getline into a Variable from a Coprocess . . . . . . . . . . 834.9.9 Points to Remember About getline . . . . . . . . . . . . . . . . . . . . . . 83

v4.9.10 Summary of getline Variants . . . . . . . . . . . . . . . . . . . . . . . . . . .4.10 Reading Input with a Timeout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.11 Directories on the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4.13 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58484868687Printing Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 895.1 The print Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 895.2 print Statement Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 895.3 Output Separators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915.4 Controlling Numeric Output with print . . . . . . . . . . . . . . . . . . . . . . . 925.5 Using printf Statements for Fancier Printing . . . . . . . . . . . . . . . . . . 925.5.1 Introduction to the printf Statement. . . . . . . . . . . . . . . . . . . . . 925.5.2 Format-Control Letters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 935.5.3 Modifiers for printf Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945.5.4 Examples Using printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965.6 Redirecting Output of print and printf . . . . . . . . . . . . . . . . . . . . . . 975.7 Special Files for Standard Preopened Data Streams . . . . . . . . . . . 1005.8 Special File names in gawk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1015.8.1 Accessing Other Open Files with gawk . . . . . . . . . . . . . . . . . . . 1015.8.2 Special Files for Network Communications . . . . . . . . . . . . . . . 1015.8.3 Special File name Caveats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1025.9 Closing Input and Output Redirections . . . . . . . . . . . . . . . . . . . . . . . 1025.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1045.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1056Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1076.1Constants, Variables, and Conversions . . . . . . . . . . . . . . . . . . . . . . . . 1076.1.1 Constant Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1076.1.1.1 Numeric and String Constants . . . . . . . . . . . . . . . . . . . . . . 1076.1.1.2 Octal and Hexadecimal Numbers . . . . . . . . . . . . . . . . . . . . 1076.1.1.3 Regular Expression Constants. . . . . . . . . . . . . . . . . . . . . . . 1086.1.2 Using Regular Expression Constants . . . . . . . . . . . . . . . . . . . . . 1096.1.3 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1106.1.3.1 Using Variables in a Program . . . . . . . . . . . . . . . . . . . . . . . 1106.1.3.2 Assigning Variables on the Command Line . . . . . . . . . . 1116.1.4 Conversion of Strings and Numbers . . . . . . . . . . . . . . . . . . . . . . 1116.1.4.1 How awk Converts Between Strings and Numbers . . . 1116.1.4.2 Locales Can Influence Conversion . . . . . . . . . . . . . . . . . . . 1126.2 Operators: Doing Something with Values . . . . . . . . . . . . . . . . . . . . . 1136.2.1 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1146.2.2 String Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1156.2.3 Assignment Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1166.2.4 Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . 1196.3 Truth Values and Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1206.3.1 True and False in awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1206.3.2 Variable Typing and Comparison Expressions . . . . . . . . . . . . 120

vi GAWK: Effective AWK Programming6.3.2.1 String Type versus Numeric Type . . . . . . . . . . . . . . . . . . . 1216.3.2.2 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1226.3.2.3 String Comparison with POSIX Rules. . . . . . . . . . . . . . . 1246.3.3 Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1246.3.4 Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1256.4 Function Calls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1266.5 Operator Precedence (How Operators Nest). . . . . . . . . . . . . . . . . . . 1286.6 Where You Are Makes a Difference . . . . . . . . . . . . . . . . . . . . . . . . . . . 1296.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1297Patterns, Actions, and Variables . . . . . . . . . . . . 1317.1Pattern Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1317.1.1 Regular Expressions as Patterns . . . . . . . . . . . . . . . . . . . . . . . . . 1317.1.2 Expressions as Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1317.1.3 Specifying Record Ranges with Patterns . . . . . . . . . . . . . . . . . 1337.1.4 The BEGIN and END Special Patterns . . . . . . . . . . . . . . . . . . . . . 1347.1.4.1 Startup and Cleanup Actions . . . . . . . . . . . . . . . . . . . . . . . 1347.1.4.2 Input/Output from BEGIN and END Rules. . . . . . . . . . . . 1357.1.5 The BEGINFILE and ENDFILE Special Patterns. . . . . . . . . . . . 1357.1.6 The Empty Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1367.2 Using Shell Variables in Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1367.3 Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1377.4 Control Statements in Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1387.4.1 The if-else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1387.4.2 The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1397.4.3 The do-while Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1407.4.4 The for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1407.4.5 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1417.4.6 The break Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1427.4.7 The continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1437.4.8 The next Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1447.4.9 The nextfile Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1457.4.10 The exit Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1467.5 Predefined Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1477.5.1 Built-in Variables That Control awk. . . . . . . . . . . . . . . . . . . . . . 1477.5.2 Built-in Variables That Convey Information . . . . . . . . . . . . . . 1497.5.3 Using ARGC and ARGV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1557.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1578Arrays in awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1598.1The8.1.18.1.28.1.38.1.48.1.58.1.6Basics of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159Introduction to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159Referring to an Array Element . . . . . . . . . . . . . . . . . . . . . . . . . . . 161Assigning Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162Basic Array Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162Scanning All Elements of an Array. . . . . . . . . . . . . . . . . . . . . . . 163Using Predefined Array Scanning Orders with gawk . . . . . . 164

vii8.28.38.48.5Using Numbers to Subscript Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . 167Using Uninitialized Variables as Subscripts. . . . . . . . . . . . . . . . . . . . 167The delete Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1698.5.1 Scanning Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . 1718.6 Arrays of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1718.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1739Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1759.1Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1759.1.1 Calling Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1759.1.2 Numeric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1769.1.3 String-Manipulation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 1779.1.3.1 More about ‘\’ and ‘&’ withsub(), gsub(), and gensub() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1869.1.4 Input/Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1899.1.5 Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1929.1.6 Bit-Manipulation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1979.1.7 Getting Type Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1999.1.8 String-Translation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1999.2 User-Defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2009.2.1 Function Definition Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2009.2.2 Function Definition Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2029.2.3 Calling User-Defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 2039.2.3.1 Writing a Function Call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2039.2.3.2 Controlling Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . 2039.2.3.3 Passing Function Arguments by Value Or by Reference . . 2069.2.4 The return Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2079.2.5 Functions and Their Effects on Variable Typing . . . . . . . . . . 2089.3 Indirect Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2099.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214Part II: Problem Solving with awk10A Library of awk Functions . . . . . . . . . . . . . . . . . 21910.1 Naming Library Function Global Variables . . . . . . . . . . . . . . . . . . 22010.2 General Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22110.2.1 Converting Strings to Numbers . . . . . . . . . . . . . . . . . . . . . . . . . 22110.2.2 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22210.2.3 Rounding Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22410.2.4 The Cliff Random Number Generator . . . . . . . . . . . . . . . . . . . 22510.2.5 Translating Between Characters and Numbers . . . . . . . . . . 22510.2.6 Merging an Array into a String . . . . . . . . . . . . . . . . . . . . . . . . . 22710.2.7 Managing the Time of Day . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22710.2.8 Reading a Whole File at Once . . . . . . . . . . . . . . . . . . . . . . . . . . 22910.2.9 Quoting Strings to Pass to the Shell . . . . . . . . . . . . . . . . . . . . 23010.3 Data file Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231

viii GAWK: Effective AWK Programming10.3.1 Noting Data file Boundaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23110.3.2 Rereading the Current File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23210.3.3 Checking for Readable Data files. . . . . . . . . . . . . . . . . . . . . . . . 23410.3.4 Checking for Zero-Length Files . . . . . . . . . . . . . . . . . . . . . . . . . 23410.3.5 Treating Assignments as File names . . . . . . . . . . . . . . . . . . . . 23510.4 Processing Command-Line Options . . . . . . . . . . . . . . . . . . . . . . . . . . 23610.5 Reading the User Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24110.6 Reading the Group Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24510.7 Traversing Arrays of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24910.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25110.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25111Practical awk Programs. . . . . . . . . . . . . . . . . . . . . . 25311.1 Running the Example Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25311.2 Reinventing Wheels for Fun and Profit . . . . . . . . . . . . . . . . . . . . . . 25311.2.1 Cutting Out Fields and Columns . . . . . . . . . . . . . . . . . . . . . . . 25311.2.2 Searching for Regular Expressions in Files . . . . . . . . . . . . . . 25811.2.3 Printing Out User Information. . . . . . . . . . . . . . . . . . . . . . . . . . 26211.2.4 Splitting a Large File into Pieces . . . . . . . . . . . . . . . . . . . . . . . 26411.2.5 Duplicating Output into Multiple Files . . . . . . . . . . . . . . . . . 26511.2.6 Printing Nonduplicated Lines of Text . . . . . . . . . . . . . . . . . . . 26711.2.7 Counting Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27111.3 A Grab Bag of awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27311.3.1 Finding Duplicated Words in a Document . . . . . . . . . . . . . . 27311.3.2 An Alarm Clock Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27411.3.3 Transliterating Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27611.3.4 Printing Mailing Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27811.3.5 Generating Word-Usage Counts. . . . . . . . . . . . . . . . . . . . . . . . . 28011.3.6 Removing Duplicates from Unsorted Text . . . . . . . . . . . . . . . 28211.3.7 Extracting Programs from Texinfo Source Files . . . . . . . . . 28311.3.8 A Simple Stream Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28611.3.9 An Easy Way to Use Library Functions . . . . . . . . . . . . . . . . . 28711.3.10 Finding Anagrams from a Dictionary . . . . . . . . . . . . . . . . . . 29411.3.11 And Now for Something Completely Different . . . . . . . . . . 29511.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29611.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296Part III: Moving Beyond Standard awk with gawk12Advanced Features of gawk . . . . . . . . . . . . . . . . . . 30112.1 Allowing Nondecimal Input Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30112.2 Controlling Array Traversal and Array Sorting . . . . . . . . . . . . . . . 30212.2.1 Controlling Array Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30212.2.2 Sorting Array Values and Indices with gawk . . . . . . . . . . . . . 30612.3 Two-Way Communications with Another Process . . . . . . . . . . . . 30812.4 Using gawk for Network Programming . . . . . . . . . . . . . . . . . . . . . . . 31112.5 Profiling Your awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31212.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316

ix13Internationalization with gawk . . . . . . . . . . . . . . 31713.1 Internationalization and Localization . . . . . . . . . . . . . . . . . . . . . . . . 31713.2 GNU gettext. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31713.3 Internationalizing awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31913.4 Translating awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32113.4.1 Extracting Marked Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32113.4.2 Rearranging printf Arguments . . . . . . . . . . . . . . . . . . . . . . . . 32213.4.3 awk Portability Issues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32313.5 A Simple Internationalization Example . . . . . . . . . . . . . . . . . . . . . . 32413.6 gawk Can Speak Your Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32513.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32514Debugging awk Programs. . . . . . . . . . . . . . . . . . . . 32714.1 Introduction to the gawk Debugger . . . . . . . . . . . . . . . . . . . . . . . .

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

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

Anatomi Tulang dan Fisiologi Panggul 2.1.1 Tulang Tulang pelvis merupakan komposisi dari tiga buah tulang yakni dua tulang kokse . tulang pria lebih kekar dan kuat, sedangkan kerangka perempuan lebih ditujukan kepada pemenuhan fungsi reproduksi. Pada wanita bentuk thorak bagian bawah lebih besar, panggul berbentuk ginekoid dengan ala iliaka lebih lebar dan cekung, promontorium kurang .