User Commands awk(1) NAME awk - pattern scanning and processing language SYNOPSIS /usr/bin/awk [ -f _p_r_o_g_f_i_l_e ] [ -F _c ] [ ' _p_r_o_g ' ] [ _p_a_r_a_m_e_t_e_r_s ] [ _f_i_l_e_n_a_m_e ... ] /usr/xpg4/bin/awk [ -F _E_R_E ] [ -v _a_s_s_i_g_n_m_e_n_t ... ] '_p_r_o_- _g_r_a_m' -f _p_r_o_g_f_i_l_e ... [ _a_r_g_u_m_e_n_t ... ] DESCRIPTION The /usr/xpg4/bin/awk utility is described on the nawk(1) manual page. The /usr/bin/awk utility scans each input _f_i_l_e_n_a_m_e for lines that match any of a set of patterns specified in _p_r_o_g. The _p_r_o_g string must be enclosed in single quotes ( ') to pro- tect it from the shell. For each pattern in _p_r_o_g there may be an associated action performed when a line of a _f_i_l_e_n_a_m_e matches the pattern. The set of pattern-action statements may appear literally as _p_r_o_g or in a file specified with the -f _p_r_o_g_f_i_l_e option. Input files are read in order; if there are no files, the standard input is read. The file name '-' means the standard input. OPTIONS The following options are supported: -f _p_r_o_g_f_i_l_e awk uses the set of patterns it reads from _p_r_o_g_f_i_l_e. -F_c Uses the character _c as the field separator (FS) char- acter. See the discussion of FS below. USAGE Input Lines Each input line is matched against the pattern portion of every pattern-action statement; the associated action is performed for each matched pattern. Any _f_i_l_e_n_a_m_e of the form _v_a_r=_v_a_l_u_e is treated as an assignment, not a filename, and is executed at the time it would have been opened if it were a filename. _V_a_r_i_a_b_l_e_s assigned in this manner are not avail- able inside a BEGIN rule, and are assigned after previously specified files have been read. An input line is normally made up of fields separated by white spaces. (This default can be changed by using the FS built-in variable or the -F_c option.) The default is to ignore leading blanks and to separate fields by blanks and/or tab characters. However, if FS is assigned a value that does not include any of the white spaces, then leading blanks are not ignored. The fields are denoted $1, $2, ...; SunOS 5.8 Last change: 10 Feb 1999 1 User Commands awk(1) $0 refers to the entire line. Pattern-action Statements A pattern-action statement has the form: _p_a_t_t_e_r_n { _a_c_t_i_o_n } Either pattern or action may be omitted. If there is no action, the matching line is printed. If there is no pat- tern, the action is performed on every input line. Pattern- action statements are separated by newlines or semicolons. Patterns are arbitrary Boolean combinations ( !, ||, &&, and parentheses) of relational expressions and regular expres- sions. A relational expression is one of the following: _e_x_p_r_e_s_s_i_o_n _r_e_l_o_p _e_x_p_r_e_s_s_i_o_n _e_x_p_r_e_s_s_i_o_n _m_a_t_c_h_o_p _r_e_g_u_l_a_r__e_x_p_r_e_s_s_i_o_n where a _r_e_l_o_p is any of the six relational operators in C, and a _m_a_t_c_h_o_p is either ~ (contains) or !~ (does not con- tain). An _e_x_p_r_e_s_s_i_o_n is an arithmetic expression, a rela- tional expression, the special expression _v_a_r in _a_r_r_a_y or a Boolean combination of these. Regular expressions are as in egrep(1). In patterns they must be surrounded by slashes. Isolated regular expressions in a pattern apply to the entire line. Regular expressions may also occur in relational expressions. A pattern may con- sist of two patterns separated by a comma; in this case, the action is performed for all lines between the occurrence of the first pattern to the occurrence of the second pattern. The special patterns BEGIN and END may be used to capture control before the first input line has been read and after the last input line has been read respectively. These key- words do not combine with any other patterns. Built-in Variables Built-in variables include: FILENAME name of the current input file FS input field separator regular expression (default SunOS 5.8 Last change: 10 Feb 1999 2 User Commands awk(1) blank and tab) NF number of fields in the current record NR ordinal number of the current record OFMT output format for numbers (default %.6g) OFS output field separator (default blank) ORS output record separator (default new-line) RS input record separator (default new-line) An action is a sequence of statements. A statement may be one of the following: if ( _e_x_p_r_e_s_s_i_o_n ) _s_t_a_t_e_m_e_n_t [ else _s_t_a_t_e_m_e_n_t ] while ( _e_x_p_r_e_s_s_i_o_n ) _s_t_a_t_e_m_e_n_t do _s_t_a_t_e_m_e_n_t while ( _e_x_p_r_e_s_s_i_o_n ) for ( _e_x_p_r_e_s_s_i_o_n ; _e_x_p_r_e_s_s_i_o_n ; _e_x_p_r_e_s_s_i_o_n ) _s_t_a_t_e_m_e_n_t for ( _v_a_r in _a_r_r_a_y ) _s_t_a_t_e_m_e_n_t break continue { [ _s_t_a_t_e_m_e_n_t ] ... } _e_x_p_r_e_s_s_i_o_n # commonly variable = expression print [ _e_x_p_r_e_s_s_i_o_n-_l_i_s_t ] [ >_e_x_p_r_e_s_s_i_o_n ] printf format [ ,_e_x_p_r_e_s_s_i_o_n-_l_i_s_t ] [ >_e_x_p_r_e_s_s_i_o_n ] next # skip remaining patterns on this input line exit [expr] # skip the rest of the input; exit status is expr Statements are terminated by semicolons, newlines, or right braces. An empty expression-list stands for the whole input line. Expressions take on string or numeric values as appropriate, and are built using the operators +, -, *, /, %, ^ and concatenation (indicated by a blank). The opera- tors ++, --, +=, -=, *=, /=, %=, ^=, >, >=, <, <=, ==, !=, and ?: are also available in expressions. Variables may be scalars, array elements (denoted x[i]), or fields. Variables are initialized to the null string or zero. Array subscripts may be any string, not necessarily numeric; this allows for a form of associative memory. String constants are quoted (""), with the usual C escapes recognized within. The print statement prints its arguments on the standard output, or on a file if >_e_x_p_r_e_s_s_i_o_n is present, or on a pipe if '|_c_m_d' is present. The output resulted from the print statement is terminated by the output record separator with each argument separated by the current output field separa- tor. The printf statement formats its expression list according to the format (see printf(3C)). SunOS 5.8 Last change: 10 Feb 1999 3 User Commands awk(1) Built-in Functions The arithmetic functions are as follows: cos(_x) Return cosine of _x, where _x is in radians. sin(_x) Return sine of _x, where _x is in radians. exp(_x) Return the exponential function of _x. log(_x) Return the natural logarithm of _x. sqrt(_x) Return the square root of _x. int(_x) Truncate its argument to an integer. It will be trun- cated toward 0 when _x > 0. The string functions are as follows: index(_s, _t) Return the position in string _s where string _t first occurs, or 0 if it does not occur at all. int(_s) truncates _s to an integer value. If _s is not speci- fied, $0 is used. length(_s) Return the length of its argument taken as a string, or of the whole line if there is no argument. match(_s, _r_e) Return the position in string _s where the regular expression _r_e occurs, or 0 if it does not occur at all. split(_s, _a, _f_s) Split the string _s into array elements _a[_1], _a[_2], ... _a[_n], and returns _n. The separation is done with the regular expression _f_s or with the field separator FS if _f_s is not given. sprintf(_f_m_t, _e_x_p_r, _e_x_p_r,...) Format the expressions according to the printf(3C) format given by _f_m_t and returns the resulting string. substr(_s, _m, _n) SunOS 5.8 Last change: 10 Feb 1999 4 User Commands awk(1) returns the _n-character substring of _s that begins at position _m. The input/output function is as follows: getline Set $0 to the next input record from the current input file. getline returns 1 for successful input, 0 for end of file, and -1 for an error. Large File Behavior See largefile(5) for the description of the behavior of awk when encountering files greater than or equal to 2 Gbyte ( 2 **31 bytes). EXAMPLES Example 1: Print lines longer than 72 characters: length > 72 Example 2: Print first two fields in opposite order: { print $2, $1 } Example 3: Same, with input fields separated by comma and/or blanks and tabs: BEGIN { FS = ",[ \t]*|[ \t]+" } { print $2, $1 } Example 4: Add up first column, print sum and average: { s += $1 } END { print "sum is", s, " average is", s/NR } Example 5: Printing fields in reverse order { for (i = NF; i > 0; --i) print $i } Example 6: Print all lines between start/stop pairs: /start/, /stop/ Example 7: Print all lines whose first field is different from the previous one: $1 != prev { print; prev = $1 } Example 8: Print a file, filling in page numbers starting at 5: SunOS 5.8 Last change: 10 Feb 1999 5 User Commands awk(1) /Page/ { $2 = n++; } { print } Example 9: Print a file and number its pages starting at 5: Assuming this program is in a file named prog, the following command line prints the file input numbering its pages starting at 5: awk f prog n=5 input ENVIRONMENT VARIABLES See environ(5) for descriptions of the following environment variables that affect the execution of awk: LC_CTYPE and LC_MESSAGES. LC_NUMERIC Determine the radix character used when interpreting numeric input, performing conversions between numeric and string values and formatting numeric output. Regardless of locale, the period character (the decimal-point character of the POSIX locale) is the decimal-point character recognized in processing awk programs (including assignments in command-line argu- ments). ATTRIBUTES See attributes(5) for descriptions of the following attri- butes: /usr/bin/awk ____________________________________________________________ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | |______________________________|______________________________| | Availability | SUNWesu | |______________________________|______________________________| | CSI | Enabled | |______________________________|______________________________| /usr/xpg4/bin/awk ____________________________________________________________ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | |______________________________|______________________________| | Availability | SUNWxcu4 | |______________________________|______________________________| | CSI | Enabled | |______________________________|______________________________|