|
|
1.1 root 1:
2:
3: awk Command awk
4:
5:
6:
7:
8: Pattern-scanning language
9:
10: aawwkk [-yy][-FF _c][-ff _p_r_o_g_f_i_l_e][_p_r_o_g][_f_i_l_e ...]
11:
12: awk is a general-purpose language designed for processing input
13: data. Its features allow you to write programs that scan for
14: patterns, produce reports, and filter relevant information from a
15: mass of input data. It acts on each input file following the
16: commands you write into an awk program. awk reads the standard
17: input if no file is specified, which allows it to act as a filter
18: in a pipeline; the file name `-' means the standard input.
19:
20: You can specify the program either as an argument prog (usually
21: enclosed in quotation marks to prevent interpretation by the
22: shell sh) or in the form -f progfile, where progfile contains the
23: awk program. If no -f option appears, the first non-option argu-
24: ment is the awk program prog.
25:
26: The option flag -y specifies that any lower-case alphabetic
27: character in a regular expression pattern should match both it-
28: self and the corresponding upper-case letter. This is identical
29: to the matching found in the pattern-matching program grep with
30: the -y option.
31:
32: awk views its input as a sequence of records, each consisting of
33: zero or more fields. By default, newlines separate records and
34: white space (spaces or tabs) separates fields. The option -Fc
35: changes the input field separator characters to the characters in
36: the string c. An awk program can also change the field and
37: record separators. The program can access the values of each
38: field and the entire record through built-in variables.
39:
40: For details on the construction of awk programs, consult the
41: tutorial to aawwkk that appears in this manual. Briefly, an awk
42: program consists of one or more lines, each containing a pattern
43: or an action, or both. A pattern determines whether awk performs
44: the associated action. It may consist of regular expressions,
45: line ranges, boolean combinations of variables, and beginning and
46: end of input-text predicates. If no pattern is specified, awk
47: executes the action (the pattern matches by default).
48:
49: An action is enclosed in braces. The syntax of actions is C-
50: like, and consists of simple and compound statements constructed
51: from constants (numbers, strings), input fields, built-in and
52: user-defined variables, and built-in functions. If an action is
53: missing, awk prints the entire input record (line).
54:
55: Unlike lex or yacc, awk does not compile programs into an execut-
56: able image, but interprets them directly. Thus, awk is ideal for
57: quickly-implemented, one-shot efforts.
58:
59: ***** Examples *****
60:
61: The following examples illustrate the economy of expression of
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: awk Command awk
70:
71:
72:
73: awk programs.
74:
75: The first example prints all lines containing the string
76: ``COHERENT'' (identical to ggrreepp CCOOHHEERREENNTT):
77:
78:
79: /COHERENT/
80:
81:
82: The built-in variable NR is the number of the current input
83: record, so the following program prints the number of records
84: (lines) in the input stream.
85:
86:
87: END { print NR }
88:
89:
90: The built-in variable $3 gives the value of the third field of
91: the current record, so the following program sums the elements in
92: column three of an input table and prints the total:
93:
94:
95: { sum += $3 }
96: END { print sum }
97:
98:
99: ***** See Also *****
100:
101: commands, grep, lex, sed, yacc
102: _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
103:
104: ***** Notes *****
105:
106: There is no way to have a null field, such as is necessary to
107: describe the colon-separated fields in /etc/passwd.
108:
109: awk converts between strings and numbers automatically. Adding
110: zero to a string forces awk to treat it as a number; con-
111: catenating "" to a number forces awk to treat it as a string.
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130: COHERENT Lexicon Page 2
131:
132:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.