Awk Patterns
The core syntax of awk(1p) is pattern { action }. This page describes patterns.
Regular Expressions
Regular expressions are essential to awk(1p). They are a builtin type and are expressed as strings delimited by forward slashes (/).
An entire record can be matched against a regular expression as:
/^[0-9]+ / { print $2 }
A variable or field can be matched against a regular expression as:
if ($1 ~ /^[0-9]+$/) { print $2 }
Begin and End
The special patterns BEGIN and END are for initialization and clean-up actions.
To initialize a variable that will be referenced in a program, try:
BEGIN { ansi_reset="\033[0m"; }
To count the number of records (i.e. lines) in a file, try:
END { print NR }
Empty Patterns
An empty pattern matches every line. This can be written in one of two ways:
{ print "No pattern at all" } // { print "Blank regexp pattern" }