- •Preface
- •History of awk
- •GNU GENERAL PUBLIC LICENSE
- •Preamble
- •TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
- •How to Apply These Terms to Your New Programs
- •Using this Manual
- •Data Files for the Examples
- •Getting Started with awk
- •A Very Simple Example
- •An Example with Two Rules
- •A More Complex Example
- •How to Run awk Programs
- •One-shot Throw-away awk Programs
- •Running awk without Input Files
- •Running Long Programs
- •Executable awk Programs
- •Comments in awk Programs
- •awk Statements versus Lines
- •When to Use awk
- •Reading Input Files
- •How Input is Split into Records
- •Examining Fields
- •Non-constant Field Numbers
- •Changing the Contents of a Field
- •Specifying how Fields are Separated
- •Multiple-Line Records
- •Explicit Input with getline
- •Closing Input Files and Pipes
- •Printing Output
- •The print Statement
- •Examples of print Statements
- •Output Separators
- •Controlling Numeric Output with print
- •Using printf Statements for Fancier Printing
- •Introduction to the printf Statement
- •Format-Control Letters
- •Examples of Using printf
- •Redirecting Output of print and printf
- •Redirecting Output to Files and Pipes
- •Closing Output Files and Pipes
- •Standard I/O Streams
- •Patterns
- •Kinds of Patterns
- •Regular Expressions as Patterns
- •How to Use Regular Expressions
- •Regular Expression Operators
- •Case-sensitivity in Matching
- •Comparison Expressions as Patterns
- •Boolean Operators and Patterns
- •Expressions as Patterns
- •Specifying Record Ranges with Patterns
- •BEGIN and END Special Patterns
- •The Empty Pattern
- •Overview of Actions
- •Expressions as Action Statements
- •Constant Expressions
- •Variables
- •Assigning Variables on the Command Line
- •Arithmetic Operators
- •String Concatenation
- •Comparison Expressions
- •Boolean Expressions
- •Assignment Expressions
- •Increment Operators
- •Conversion of Strings and Numbers
- •Numeric and String Values
- •Conditional Expressions
- •Function Calls
- •Operator Precedence (How Operators Nest)
- •Control Statements in Actions
- •The if Statement
- •The while Statement
- •The do-while Statement
- •The for Statement
- •The break Statement
- •The continue Statement
- •The next Statement
- •The exit Statement
- •Arrays in awk
- •Introduction to Arrays
- •Referring to an Array Element
- •Assigning Array Elements
- •Basic Example of an Array
- •Scanning all Elements of an Array
- •The delete Statement
- •Using Numbers to Subscript Arrays
- •Multi-dimensional Arrays
- •Scanning Multi-dimensional Arrays
- •Built-in Functions
- •Calling Built-in Functions
- •Numeric Built-in Functions
- •Built-in Functions for String Manipulation
- •Built-in Functions for Input/Output
- •The return Statement
- •Built-in Variables
- •Built-in Variables that Control awk
- •Built-in Variables that Convey Information
- •Invoking awk
- •Command Line Options
- •Other Command Line Arguments
- •Index
Chapter 7: Overview of Actions |
55 |
7 Overview of Actions
An awk program or script consists of a series of rules and function de nitions, interspersed. (Functions are described later. See Chapter 12 [User-de ned Functions], page 95.)
A rule contains a pattern and an action, either of which may be omitted. The purpose of the action is to tell awk what to do once a match for the pattern is found. Thus, the entire program looks somewhat like this:
[pattern] [{ action }] [pattern] [{ action }]
: : :
function name (args) { : : : }
: : :
An action consists of one or more awk statements, enclosed in curly braces (`{' and `}'). Each statement speci es one thing to be done. The statements are separated by newlines or semicolons.
The curly braces around an action must be used even if the action contains only one statement, or even if it contains no statements at all. However, if you omit the action entirely, omit the curly braces as well. (An omitted action is equivalent to `{ print $0 }'.)
Here are the kinds of statements supported in awk:
Expressions, which can call functions or assign values to variables (see Chapter 8 [Expressions as Action Statements], page 57). Executing this kind of statement simply computes the value of the expression and then ignores it. This is useful when the expression has side e ects (see Section 8.7 [Assignment Expressions], page 64).
Control statements, which specify the control ow of awk programs. The awk language gives you C-like constructs (if, for, while, and so on) as well as a few special ones (see Chapter 9 [Control Statements in Actions], page 73).
Compound statements, which consist of one or more statements enclosed in curly braces. A compound statement is used in order to put several statements together in the body of an if, while, do or for statement.
Input control, using the getline command (see Section 3.7 [Explicit Input with getline], page 30), and the next statement (see Section 9.7 [The next Statement], page 78).
Output statements, print and printf. See Chapter 4 [Printing Output], page 35.
Deletion statements, for deleting array elements. See Section 10.6 [The delete Statement], page 85.
The next two chapters cover in detail expressions and control statements, respectively. We go on to treat arrays and built-in functions, both of which are used in expressions. Then we proceed to discuss how to de ne your own functions.
56 |
The AWK Manual |