Chapter 6. Patterns, Actions, and Variables
As you have already seen, each awk statement consists of a pattern with an associated action. This chapter describes how you build patterns and actions, what kinds of things you can do within actions, and awk’s built-in variables.
The pattern-action rules and the statements available for use within actions form the core of awk programming. In a sense, everything covered in this text up to here has been the foundation that programs are built on top of. Now it’s time to start building something useful.
Pattern Elements
Patterns in awk control the execution of rules -- a rule is executed when its pattern matches the current input record. The following is a summary of the types of awk pattern types:
-
/
regular expression
/
A regular expression. It matches when the text of the input record fits the regular expression. (See Chapter 2.)
-
expression
A single expression. It matches when its value is nonzero (if a number) or non-null (if a string). (See Section 6.1.2 later in this chapter.)
-
pat1
,
pat2
A pair of patterns separated by a comma, specifying a range of records. The range includes both the initial record that matches pat1 and the final record that matches pat2. (See Section 6.1.3 later in this chapter.)
-
BEGIN
END
Special patterns for you to supply startup or cleanup actions for your awk program. (See Section 6.1.4 later in this chapter.)
- empty
The empty pattern matches every input record. (See Section 6.1.5 later in this ...
Get Effective awk Programming, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.