In awk, patterns are used to select lines of input for processing. The pattern within each rule describes a section within a line of text that needs to be matched, in order for the associated action to take place. The action only executes if its relevant pattern was matched or the pattern was omitted from the rule.

Regular Expression Patterns

A pattern may contain regular expressions for matching text within a string. The slash symbol is used as an [[enclosure?]] for regular expressions used in the pattern. In the following example, the action (to print the line) will occur for each line that match the regular expression foobar.

 /foobar/ {print $0}

Start and End Patterns

It is possible to create a pattern consisting of two regular expressions separated by a [[comma?]] symbol to mark start and ending points for text selection. The following will select all records between [[foo?]] and [[bar?]]:


The two patterns must be in separate records

Note that [[foo?]] selects the first matching record, [[bar?]] must be in a later record. If a single record containing both [[foo?]] and [[bar?]] is matched, then the record will be selected, but subsequent [[records?]] will also be selected, because awk will continue to look for the ending pattern [[bar?]].

Boolean Patterns

The actionwithin a rule will take place, if the [[expression?]] within the pattern evaluates to [[true?]]:

0 {print "Hello"} # This will not cause output, because a value of zero is considered to be false

1 {print "Goodbye"}  # This will cause output, because a value of one is considered to be true

Omission of the pattern

If a rule does not include a pattern, then the action within the rule is performed for every input line.