- •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 8: Expressions as Action Statements |
59 |
matches = /foo/
will assign either 0 or 1 to the variable matches, depending upon the contents of the current input record.
Constant regular expressions are also used as the rst argument for the sub and gsub functions (see Section 11.3 [Built-in Functions for String Manipulation], page 90).
This feature of the language was never well documented until the posix speci cation.
You may be wondering, when is
$1 ~ /foo/ { : : : }
preferable to
$1 ~ "foo" { : : : }
Since the right-hand sides of both `~' operators are constants, it is more e cient to use the `/foo/' form: awk can note that you have supplied a regexp and store it internally in a form that makes pattern matching more e cient. In the second form, awk must rst convert the string into this internal form, and then perform the pattern matching. The rst form is also better style; it shows clearly that you intend a regexp match.
8.2 Variables
Variables let you give names to values and refer to them later. You have already seen variables in many of the examples. The name of a variable must be a sequence of letters, digits and underscores, but it may not begin with a digit. Case is signi cant in variable names; a and A are distinct variables.
A variable name is a valid expression by itself; it represents the variable's current value. Variables are given new values with assignment operators and increment operators. See Section 8.7 [Assignment Expressions], page 64.
A few variables have special built-in meanings, such as FS, the eld separator, and NF, the number of elds in the current input record. See Chapter 13 [Built-in Variables], page 101, for a list of them. These built-in variables can be used and assigned just like all other variables, but their values are also used or changed automatically by awk. Each built-in variable's name is made entirely of upper case letters.
Variables in awk can be assigned either numeric or string values. By default, variables are initialized to the null string, which is e ectively zero if converted to a number. There is no need to \initialize" each variable explicitly in awk, the way you would in C or most other traditional languages.