Home Page Icon
Home Page
Table of Contents for
Cover
Close
Cover
by Arnold Robbins
Effective awk Programming, 4th Edition
Dedication
Foreword to the Third Edition
Foreword to the Fourth Edition
Preface
History of awk and gawk
A Rose by Any Other Name
Using This Book
Typographical Conventions
Dark Corners
The GNU Project and This Book
How to Stay Current
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
I. The awk Language
1. Getting Started with awk
How to Run awk Programs
One-Shot Throwaway awk Programs
Running awk Without Input Files
Running Long Programs
Executable awk Programs
Comments in awk Programs
Shell Quoting Issues
Quoting in MS-Windows batch files
Datafiles for the Examples
Some Simple Examples
An Example with Two Rules
A More Complex Example
awk Statements Versus Lines
Other Features of awk
When to Use awk
Summary
2. Running awk and gawk
Invoking awk
Command-Line Options
Other Command-Line Arguments
Naming Standard Input
The Environment Variables gawk Uses
The AWKPATH Environment Variable
The AWKLIBPATH Environment Variable
Other Environment Variables
gawk’s Exit Status
Including Other Files into Your Program
Loading Dynamic Extensions into Your Program
Obsolete Options and/or Features
Undocumented Options and Features
Summary
3. Regular Expressions
How to Use Regular Expressions
Escape Sequences
Regular Expression Operators
Using Bracket Expressions
How Much Text Matches?
Using Dynamic Regexps
gawk-Specific Regexp Operators
Case Sensitivity in Matching
Summary
4. Reading Input Files
How Input Is Split into Records
Record Splitting with Standard awk
Record Splitting with gawk
Examining Fields
Nonconstant Field Numbers
Changing the Contents of a Field
Specifying How Fields Are Separated
Whitespace Normally Separates Fields
Using Regular Expressions to Separate Fields
Making Each Character a Separate Field
Setting FS from the Command Line
Making the Full Line Be a Single Field
Field-Splitting Summary
Reading Fixed-Width Data
Defining Fields by Content
Multiple-Line Records
Explicit Input with getline
Using getline with No Arguments
Using getline into a Variable
Using getline from a File
Using getline into a Variable from a File
Using getline from a Pipe
Using getline into a Variable from a Pipe
Using getline from a Coprocess
Using getline into a Variable from a Coprocess
Points to Remember About getline
Summary of getline Variants
Reading Input with a Timeout
Directories on the Command Line
Summary
5. Printing Output
The print Statement
print Statement Examples
Output Separators
Controlling Numeric Output with print
Using printf Statements for Fancier Printing
Introduction to the printf Statement
Format-Control Letters
Modifiers for printf Formats
Examples Using printf
Redirecting Output of print and printf
Special Files for Standard Preopened Data Streams
Special Filenames in gawk
Accessing Other Open Files with gawk
Special Files for Network Communications
Special Filename Caveats
Closing Input and Output Redirections
Summary
6. Expressions
Constants, Variables, and Conversions
Constant Expressions
Numeric and string constants
Octal and hexadecimal numbers
Regular expression constants
Using Regular Expression Constants
Variables
Using variables in a program
Assigning variables on the command line
Conversion of Strings and Numbers
How awk converts between strings and numbers
Locales can influence conversion
Operators: Doing Something with Values
Arithmetic Operators
String Concatenation
Assignment Expressions
Increment and Decrement Operators
Truth Values and Conditions
True and False in awk
Variable Typing and Comparison Expressions
String type versus numeric type
Comparison operators
String comparison with POSIX rules
Boolean Expressions
Conditional Expressions
Function Calls
Operator Precedence (How Operators Nest)
Where You Are Makes a Difference
Summary
7. Patterns, Actions, and Variables
Pattern Elements
Regular Expressions as Patterns
Expressions as Patterns
Specifying Record Ranges with Patterns
The BEGIN and END Special Patterns
Startup and cleanup actions
Input/output from BEGIN and END rules
The BEGINFILE and ENDFILE Special Patterns
The Empty Pattern
Using Shell Variables in Programs
Actions
Control Statements in Actions
The if-else Statement
The while Statement
The do-while Statement
The for Statement
The switch Statement
The break Statement
The continue Statement
The next Statement
The nextfile Statement
The exit Statement
Predefined Variables
Built-in Variables That Control awk
Built-in Variables That Convey Information
Using ARGC and ARGV
Summary
8. Arrays in awk
The Basics of Arrays
Introduction to Arrays
Referring to an Array Element
Assigning Array Elements
Basic Array Example
Scanning All Elements of an Array
Using Predefined Array Scanning Orders with gawk
Using Numbers to Subscript Arrays
Using Uninitialized Variables as Subscripts
The delete Statement
Multidimensional Arrays
Scanning Multidimensional Arrays
Arrays of Arrays
Summary
9. Functions
Built-in Functions
Calling Built-in Functions
Numeric Functions
String-Manipulation Functions
More about ‘’ and ‘&’ with sub(), gsub(), and gensub()
Input/Output Functions
Time Functions
Bit-Manipulation Functions
Getting Type Information
String-Translation Functions
User-Defined Functions
Function Definition Syntax
Function Definition Examples
Calling User-Defined Functions
Writing a function call
Controlling variable scope
Passing function arguments by value or by reference
The return Statement
Functions and Their Effects on Variable Typing
Indirect Function Calls
Summary
II. Problem Solving with awk
10. A Library of awk Functions
Naming Library Function Global Variables
General Programming
Converting Strings to Numbers
Assertions
Rounding Numbers
The Cliff Random Number Generator
Translating Between Characters and Numbers
Merging an Array into a String
Managing the Time of Day
Reading a Whole File at Once
Quoting Strings to Pass to the Shell
Datafile Management
Noting Datafile Boundaries
Rereading the Current File
Checking for Readable Datafiles
Checking for Zero-Length Files
Treating Assignments as Filenames
Processing Command-Line Options
Reading the User Database
Reading the Group Database
Traversing Arrays of Arrays
Summary
11. Practical awk Programs
Running the Example Programs
Reinventing Wheels for Fun and Profit
Cutting Out Fields and Columns
Searching for Regular Expressions in Files
Printing Out User Information
Splitting a Large File into Pieces
Duplicating Output into Multiple Files
Printing Nonduplicated Lines of Text
Counting Things
A Grab Bag of awk Programs
Finding Duplicated Words in a Document
An Alarm Clock Program
Transliterating Characters
Printing Mailing Labels
Generating Word-Usage Counts
Removing Duplicates from Unsorted Text
Extracting Programs from Texinfo Source Files
A Simple Stream Editor
An Easy Way to Use Library Functions
Finding Anagrams from a Dictionary
And Now for Something Completely Different
Summary
III. Moving Beyond Standard awk with gawk
12. Advanced Features of gawk
Allowing Nondecimal Input Data
Controlling Array Traversal and Array Sorting
Controlling Array Traversal
Sorting Array Values and Indices with gawk
Two-Way Communications with Another Process
Using gawk for Network Programming
Profiling Your awk Programs
Summary
13. Internationalization with gawk
Internationalization and Localization
GNU gettext
Internationalizing awk Programs
Translating awk Programs
Extracting Marked Strings
Rearranging printf Arguments
awk Portability Issues
A Simple Internationalization Example
gawk Can Speak Your Language
Summary
14. Debugging awk Programs
Introduction to the gawk Debugger
Debugging in General
Debugging Concepts
awk Debugging
Sample gawk Debugging Session
How to Start the Debugger
Finding the Bug
Main Debugger Commands
Control of Breakpoints
Control of Execution
Viewing and Changing Data
Working with the Stack
Obtaining Information About the Program and the Debugger State
Miscellaneous Commands
Readline Support
Limitations
Summary
15. Arithmetic and Arbitrary-Precision Arithmetic with gawk
A General Description of Computer Arithmetic
Other Stuff to Know
Arbitrary-Precision Arithmetic Features in gawk
Floating-Point Arithmetic: Caveat Emptor!
Floating-Point Arithmetic Is Not Exact
Many numbers cannot be represented exactly
Be careful comparing values
Errors accumulate
Getting the Accuracy You Need
Try a Few Extra Bits of Precision and Rounding
Setting the Precision
Setting the Rounding Mode
Arbitrary-Precision Integer Arithmetic with gawk
Standards Versus Existing Practice
Summary
16. Writing Extensions for gawk
Introduction
Extension Licensing
How It Works at a High Level
API Description
Introduction
General-Purpose Data Types
Memory Allocation Functions and Convenience Macros
Constructor Functions
Registration Functions
Registering an extension function
Registering an exit callback function
Registering an extension version string
Customized input parsers
Customized output wrappers
Customized two-way processors
Printing Messages
Updating ERRNO
Requesting Values
Accessing and Updating Parameters
Symbol Table Access
Variable access and update by name
Variable access and update by cookie
Creating and using cached values
Array Manipulation
Array data types
Array functions
Working with all the elements of an array
How to create and populate arrays
API Variables
API version constants and variables
Informational variables
Boilerplate Code
How gawk Finds Extensions
Example: Some File Functions
Using chdir() and stat()
C Code for chdir() and stat()
Integrating the Extensions
The Sample Extensions in the gawk Distribution
File-Related Functions
Interface to fnmatch()
Interface to fork(), wait(), and waitpid()
Enabling In-Place File Editing
Character and Numeric values: ord() and chr()
Reading Directories
Reversing Output
Two-Way I/O Example
Dumping and Restoring an Array
Reading an Entire File
Extension Time Functions
API Tests
The gawkextlib Project
Summary
IV. Appendices
A. The Evolution of the awk Language
Major Changes Between V7 and SVR3.1
Changes Between SVR3.1 and SVR4
Changes Between SVR4 and POSIX awk
Extensions in Brian Kernighan’s awk
Extensions in gawk Not in POSIX awk
Common Extensions Summary
Regexp Ranges and Locales: A Long Sad Story
Major Contributors to gawk
Summary
B. Installing gawk
The gawk Distribution
Getting the gawk Distribution
Extracting the Distribution
Contents of the gawk Distribution
Compiling and Installing gawk on Unix-Like Systems
Compiling gawk for Unix-Like Systems
Additional Configuration Options
The Configuration Process
Installation on Other Operating Systems
Installation on PC Operating Systems
Compiling gawk for PC operating systems
Testing gawk on PC operating systems
Using gawk on PC operating systems
Using gawk in the Cygwin environment
Using gawk in the MSYS environment
Compiling and Installing gawk on Vax/VMS and OpenVMS
Compiling gawk on VMS
Compiling gawk dynamic extensions on VMS
Installing gawk on VMS
Running gawk on VMS
The VMS GNV project
Some VMS systems have an old version of gawk
Reporting Problems and Bugs
Other Freely Available awk Implementations
Summary
C. GNU General Public License
Index
Colophon
Copyright
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Next
Next Chapter
Effective awk Programming
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset