Description of An Awk Program: Pattern Action
Description of An Awk Program: Pattern Action
• Its ability to view a text file as made up of records and fields in a textual
database.
• Its use of variables to manipulate the database.
• Its use of arithmetic and string operators.
• Its use of common programming constructs such as loops and conditionals.
• Its ability to generate formatted reports.
An awk program consists of one or more program lines containing a pattern and/or
action in the following format:
pattern { action }
The pattern selects lines from the input file. The awk utility performs the action on all
lines that the pattern selects. You must enclose the action within braces so that awk
can differentiate it from the pattern. There are two rules which occur if either a
pattern or action is ommited:
• If a program line does not contain a pattern, awk selects all lines in the input
file.
• If the program line does not contain an action, awk copies the selected lines to
its standard output (this is usually the display, if you haven't redirected the
output to another program or to a file).
To start, awk compares the first line in the input file with each pattern in the program.
If a pattern selects a line (if there is a match), awk takes the action associated with the
pattern. If the line is not selected, awk takes no action. When awk has completed its
comparisons for the first line of the input file, it repeats the process for the next line of
input. It continues this process, comparing subsequent lines in the input file, until it
has read the entire input file/s.
If several patterns select the same line, awk takes the actions associated with each of
the patterns in the order they appear. It is therefore possible for awk to send a single
line from the input file to its standad output more than once
Operator Meaning
You can combine any of the patterns described above using the Boolean operators ||
(OR) or && (AND).
The comma is the range operator. If you separate two patterns with a comma on a
single awk progam line, awk selects a range of lines beginning with the first line that
contains the first pattern. The last line awk selects is the next subsequent line that
contains the second pattern. After awk finds the second pattern, it starts the process
over by looking for the first pattern again.
Two unique patterns, BEGIN and END, allow you to execute commands before awk
starts its processing and after it finishes. The awk utility exeutes the actions associated
with the BEGIN pattern before, and with the END pattern after, it processes all the
files for input.
Actions
The action portion of an awk command causes awk to take action when it matches a
pattern. If you do not specify an action awk performs the default action, which is the
Print command (explicitly represented as {print}). This action copies the record
(normally a line) from the input file to awk's standard output.
You can follow a Print command with arguments, causing awk to print just the
arguements you specify. The arguments can be variables or string constants. Using
awk, you can send the output from a Print command to a file (>), append it to a file
(>>), or pipe it to the input of another program (|).
Unless you separate items in a Print command with commas, awk catenates them.
Commas cause awk to separate the items with the output field seperator (normally a
space).
You can include several actions on one line within a set of braces by seperating them
with semicolons.
Awk Command Syntax
The awk command has the following command syntax :-
When using the awk command there are two important aspects we have to specify.
We have to tell awk which data we wish to process (the input data) and then how we
wish to process it (the awk program instructions). Awk lets us do this in several
ways...
• We can point awk to a file or several files containing the input data we wish to
process
(these are shown in the above diagram as the "list of input files" )
• We can use standard input to specify the input data
(this input data may be taken from the keyboard after executing the awk
command or from another program or unix command that is piped into awk)
In order to complete this tutorial package you will be required to program some awk
code through a WWW front end. You will not need to know all the details mentioned
above as the WWW user interface is much friendlier than UNIX's (although it would
be beneficial).
For example,
# THIS AWK PROGRAM IS EQUIVILENT TO THE UNIX COMMAND "cat" (ALBEIT SLOWER!!)
{ print }
Awk Functions
Awk provides us with several built in functions for manipulating numbers and strings.
Arithmetic Operators
Awk takes its arithmetic operators from the C programming language. The following
list describes what each one does.
Printf Statement
You can use the printf command in place of print to control the format of the output
that awk generates. The awk version of printf is similar to that of the C language. A
printf command takes the following format:
printf "control-string" arg1, arg2, ... , argn
The control-string determines how printf will format arg1 - argn. These arguments
can be variables or other expressions. Within the control-string, you can use "\n" to
indicate a NEWLINE and "\t" to indicate a TAB.
%[-][x[.y]]conv
The - causes printf to left justify the argument. The x is the minimum field width, and
the .y is the number of places to the right of a decimal point in a number. The conv is
a letter from the following list:
Conv Conversion
d decimal
e exponential notation
f floating point number
g use f or e, whichever is shorter
o unsigned octal
s string of characters
x unsigned hexadecimal