An Awk Primer/Awk Quick Reference Guide

This final section provides a convenient lookup reference for Awk programming.

awk [-F&lt;ch&gt;] {pgm} | {-f &lt;pgm file&gt;} [&lt;vars&gt;] [-|&lt;data file&gt;]
 * Invoking Awk:

—where: ch:         Field-separator character. pgm:        Awk command-line program. pgm file:   File containing an Awk program. vars:       Awk variable initializations. data file:  Input data file. BEGIN             {&lt;initializations&gt;}     &lt;search pattern 1&gt; {&lt;program actions&gt;}     &lt;search pattern 2&gt; {&lt;program actions&gt;}     ... END               {&lt;final actions&gt;}
 * General form of Awk program:

/&lt;string&gt;/    Search for string. /^&lt;string&gt;/   Search for string at beginning of line. /&lt;string&gt;$/   Search for string at end of line. The search can be constrained to particular fields: $&lt;field&gt; ~ /&lt;string&gt;/  Search for string in specified field. $&lt;field&gt; !~ /&lt;string&gt;/ Search for string \Inot\i in specified field.
 * Search patterns:

Strings can be ORed in a search: /(&lt;string1&gt;)|(&lt;string2&gt;)/ The search can be for an entire range of lines, bounded by two strings: /&lt;string1&gt;/,/&lt;string2&gt;/

The search can be for any condition, such as line number, and can use the following comparison operators: == != &lt; &gt; &lt;= &gt;= Different conditions can be ORed with "||" or ANDed with "&amp;&amp;". [&lt;charlist or range&gt;]  Match on any character in list or range. [^&lt;charlist or range&gt;] Match on any character not in list or range. .                      Match any single character. *                      Match 0 or more occurrences of preceding string. ?                      Match 0 or 1 occurrences of preceding string. +                      Match 1 or more occurrences of preceding string.

If a metacharacter is part of the search string, it can be "escaped" by preceding it with a "\".

\n    Newline (line feed). Backspace. \r    Carriage return. \f    Form feed. A "\" can be embedded in a string by entering it twice: "\\".
 * Special characters:

$0; $1,$2,$3,... Field variables. NR               Number of records (lines). NF               Number of fields. FILENAME         Current input filename. FS               Field separator character (default: " "). RS               Record separator character (default: "\n"). OFS              Output field separator (default: " "). ORS              Output record separator (default: "\n"). OFMT             Output format (default: "%.6g").
 * Built-in variables:
 * Arithmetic operations:

+  Addition. -  Subtraction. *  Multiplication. /  Division. %  Mod. ++ Increment. -- Decrement. Shorthand assignments: x += 2 -- is the same as:  x = x + 2    x -= 2  -- is the same as:  x = x - 2    x *= 2  -- is the same as:  x = x * 2    x /= 2  -- is the same as:  x = x / 2    x %= 2  -- is the same as:  x = x % 2 sqrt    Square root. log     Base \Ie\i log. exp     Power of \Ie\i. int     Integer part of argument.
 * The only unique string operation is concatenation, which is performed simply by listing two strings connected by a blank space.
 * Arithmetic functions:
 * String functions:
 * length

Length of string.


 * substr(&lt;string&gt;,&lt;start of substring&gt;,&lt;max length of substring&gt;)

Get substring.


 * split(&lt;string&gt;,&lt;array&gt;,[&lt;field separator&gt;])

Split string into array, with initial array index being 1.


 * index(&lt;target string&gt;,&lt;search string&gt;)

Find index of search string in target string.


 * sprintf

Perform formatted print into string.

if (&lt;condition&gt;) &lt;action 1&gt; [else &lt;action 2&gt;]   while (&lt;condition&gt;) &lt;action&gt;
 * Control structures:

for (&lt;initial action&gt;;&lt;condition&gt;;&lt;end-of-loop action&gt;) &lt;action&gt; Scanning through an associative array with "for": for (&lt;variable&gt; in &lt;array&gt;) &lt;action&gt;

Unconditional control statements: break      Break out of "while" or "for" loop. continue   Perform next iteration of "while" or "for" loop. next       Get and scan next line of input. exit       Finish reading input and perform END statements.

print &lt;i1&gt;, &lt;i2&gt;, ...  Print items separated by OFS; end with newline. print &lt;i1&gt; &lt;i2&gt; ...    Print items concatenated; end with newline.
 * Print:
 * Printf:

General format: printf(&lt;string with format codes&gt;,[&lt;parameters&gt;]) Newlines must be explicitly specified with a "\n".

General form of format code: %[&lt;number&gt;]&lt;format code&gt;

The optional "number" can consist of:


 * A leading "-" for left-justified output.
 * An integer part that specifies the minimum output width. (A leading "0"

causes the output to be padded with zeroes.)


 * A fractional part that specifies either the maximum number of characters   to be printed (for a string), or the number of digits to be printed to the    right of the decimal point (for floating-point formats).

The format codes are: d   Prints a number in decimal format. o   Prints a number in octal format. x   Prints a number in hexadecimal format. c   Prints a character, given its numeric code. s   Prints a string. e   Prints a number in exponential format. f   Prints a number in floating-point format. g   Prints a number in exponential or floating-point format.
 * Awk can perform output redirection (using "&gt;" and "&gt;&gt;") and piping (using "|") from both "print" and "printf".