|
|
coherent
awk Command awk
Pattern-scanning language
aawwkk [-yy][-FF _c][-ff _p_r_o_g_f_i_l_e][_p_r_o_g][_f_i_l_e ...]
awk is a general-purpose language designed for processing input
data. Its features allow you to write programs that scan for
patterns, produce reports, and filter relevant information from a
mass of input data. It acts on each input file following the
commands you write into an awk program. awk reads the standard
input if no file is specified, which allows it to act as a filter
in a pipeline; the file name `-' means the standard input.
You can specify the program either as an argument prog (usually
enclosed in quotation marks to prevent interpretation by the
shell sh) or in the form -f progfile, where progfile contains the
awk program. If no -f option appears, the first non-option argu-
ment is the awk program prog.
The option flag -y specifies that any lower-case alphabetic
character in a regular expression pattern should match both it-
self and the corresponding upper-case letter. This is identical
to the matching found in the pattern-matching program grep with
the -y option.
awk views its input as a sequence of records, each consisting of
zero or more fields. By default, newlines separate records and
white space (spaces or tabs) separates fields. The option -Fc
changes the input field separator characters to the characters in
the string c. An awk program can also change the field and
record separators. The program can access the values of each
field and the entire record through built-in variables.
For details on the construction of awk programs, consult the
tutorial to aawwkk that appears in this manual. Briefly, an awk
program consists of one or more lines, each containing a pattern
or an action, or both. A pattern determines whether awk performs
the associated action. It may consist of regular expressions,
line ranges, boolean combinations of variables, and beginning and
end of input-text predicates. If no pattern is specified, awk
executes the action (the pattern matches by default).
An action is enclosed in braces. The syntax of actions is C-
like, and consists of simple and compound statements constructed
from constants (numbers, strings), input fields, built-in and
user-defined variables, and built-in functions. If an action is
missing, awk prints the entire input record (line).
Unlike lex or yacc, awk does not compile programs into an execut-
able image, but interprets them directly. Thus, awk is ideal for
quickly-implemented, one-shot efforts.
***** Examples *****
The following examples illustrate the economy of expression of
COHERENT Lexicon Page 1
awk Command awk
awk programs.
The first example prints all lines containing the string
``COHERENT'' (identical to ggrreepp CCOOHHEERREENNTT):
/COHERENT/
The built-in variable NR is the number of the current input
record, so the following program prints the number of records
(lines) in the input stream.
END { print NR }
The built-in variable $3 gives the value of the third field of
the current record, so the following program sums the elements in
column three of an input table and prints the total:
{ sum += $3 }
END { print sum }
***** See Also *****
commands, grep, lex, sed, yacc
_I_n_t_r_o_d_u_c_t_i_o_n _t_o _t_h_e _a_w_k _L_a_n_g_u_a_g_e
***** Notes *****
There is no way to have a null field, such as is necessary to
describe the colon-separated fields in /etc/passwd.
awk converts between strings and numbers automatically. Adding
zero to a string forces awk to treat it as a number; con-
catenating "" to a number forces awk to treat it as a string.
COHERENT Lexicon Page 2
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.