The awk command finds files for text containing patterns. If the line or text matches, awk performs the specified action on that line or text.
The Program statement tells awk what to do. A program instruction consists of a series of "rules", each of which specifies a pattern to look for and an action to take when a particular pattern is found.
A regular expression enclosed in slashes (/) is an awk pattern that matches input records whose text belongs to this set.
Awk Patterns
Awk patterns control whether the associated action is executed or not.
Awk supports different types of patterns, including regular expression patterns, relational patterns, ranges, and special expression patterns.
If the rule has no pattern, each input record will be matched. Here is an example rule with only one action:
$ awk '{ print $3 }' nicktest.txt
The above command will print the third field of each record.

Regular expression patterns
A regular expression or regex is a pattern that matches a set of strings. It is surrounded by forwarding slashes.
The simplest example is a real character or string matching. For example, if you want to display the first field of each record containing "5", run the following command:
$ awk '/5/ { print $1 }' nicktest.txt

The pattern can be any extended regular expression. Here is an example that prints the first field if the record starts with 2 or more digits.
$ awk '/^[0-9][0-9]/ { print $1 }' nicktest.txt
Relational expressions patterns
Relational expression patterns are generally used to match the contents of a particular field or variable.
By default, regular expression patterns are matched against records. To match a regular expression against a field, define the field and use the "contains" comparison operator (~) in the pattern.
For example, to print the first field of each record whose second field contains "ia" you would type:
$ awk '$2 ~ /a/ { print $1 }' nicktest.txt

To match fields that do not contain a given pattern use the !~ operator:
$ awk '$2 !~ /a/ { print $1 }' nicktest.txt

You can compare strings or numbers for relationships such as greater than, less than, equal, and so on. The following command prints the first field of all records whose third field is greater than 50:
$ awk '$4 > $10000 { print $1 }' nicktest.txt

Range patterns
Range patterns consist of two patterns separated by a comma:
pattern1, pattern2
All records start with a record that matches the first pattern until a record that matches the second pattern is matched.
Here is an example that will print the first field of all records starting from the record including "TSE" until the record including "Sales":
$ awk '/TSE/,/Sales/ { print $1 }' nicktest.txt

The pattern can also be a relational expression. The following command prints all records starting with the record with 15000 in the fourth field and ending with the record with 20000 in the fourth field.
$ awk '$4 == 10000, $4 == 20000 { print $0 }' nicktest.txt

Important Note: Range patterns can't be mixed with different pattern expressions.
Special expression patterns
Awk includes the following unique patterns:
BEGIN - used to act before the record is processed.
END - used to act after the record has been processed.
The BEGIN pattern is typically used to set variables, and the END pattern is used to process data from records such as Calculations.
The following example prints Start Processing, then the third field of each record, and finally End Processing.
$ awk 'BEGIN { print "Start Processing." }; { print $3 }; END { print "End Processing." }' nicktest.txt

If the program has only a BEGIN pattern, the action is executed, and the input is not processed. If the program only has an END pattern, the input will be processed before executing the rule actions.
That's all.
