Pattern Searching and Processing
awk
Batch editor may be used as a filter in one of 2 ways:
awk [-Fc] -f program-file [ file-list ]
awk program [ file-list ]
- Processes every line of every file in file-list, or standard input
if no file-list is specified
- Editing instructions are contained in a program, and are designed to
be C-like
- program-file may contain several lines of instructions, executed in
sequence
- program is a single argument, usually enclosed in ''
- -Fc specifies input field separator c (to be used in place of default
space and tab)
awk programs
A program contains one or more lines with the format:
pattern { action }
- If pattern is missing, awk applies action to all input lines.
- If action is missing, awk copies selected lines to standard output.
The awk utility works as follows:
- Execute all actions associated with BEGIN
- Process each line of input, one at a time
- Apply in sequence all actions whose associated patterns match the line.
- If no action is specified for a matching pattern, copy the line to
standard output.
- Execute all actions associated with END.
Patterns
Patterns may match either specified fields, variable values, or the
entire input line.
- Patterns may be specified with regular expressions appearing between
/ /
- ~ tests if a field or variable matches a regular expression
- !~ tests if a field or variable does not match a regular expression
- Relational operators may also be used in comparisons
< <= == != >= >
- Boolean operators may combine comparisons
|| &&
- A range may be specified by separating two patterns with a comma
Variables
User-specified variables are created and initialized when you use them.
Other variables available for your use:
| NR |
record (line) number of current record |
| $0 |
the current record (input line) |
| NF |
number of fields in the current record |
| $1 - $n |
specific fields in the current record |
| FS |
input field separator (default: space or tab) |
| OFS |
output field separator (default: space) |
| RS |
input record separator (default: newline) |
| ORS |
output record separator (default: newline) |
| FILENAME |
name of current input file |
Printing
print may be specified explicitly or implied (if no action
is specified)
- By itself, prints the current record
- Arguments (variables or string literals) may be specified
- if separated with commas, output as separate fields
- otherwise arguments are catenated
- Output may be redirected (> or >>) or piped to the input of
another program ( | )
- printf uses C-like formatting
Actions
In addition to printing, awk support several other actions.
- Functions manipulate numbers and strings
- C-like operators do arithmetic and variable assignment
- Associative arrays are indexed by strings