begin. Examples Hello World Here is the customary "Hello, world" program written in AWK: BEGIN { print "Hello, world!" exit } Print lines longer than 80 characters Print all lines longer than 80 characters. Note that the default action is to print the current line. length($0) > 80 Count words Count words in the input and print the number of lines, words, and characters (like wc): { words += NF chars += length + 1 # add one to account for the newline character at the end of each record (line) } END { print NR, words, chars } As there is no pattern for the first line of the program, every line of input matches by default, so the increment actions are executed for every line. Note that words += NF is shorthand for words = words + NF. Sum last word { s += $NF } END { print s + 0 } s is incremented by the numeric value of $NF, which is the last word on the line as defined by AWK's field separator (by default, white-space). NF is the number of fields in the current line, e.g. 4. Since $4 is the value of the fourth field, $NF is the value of the last field in the line regardless of how many fields this line has, or whether it has more or fewer fields than surrounding lines. $ is actually a unary operator with the highest operator precedence. (If the line has no fields, then NF is 0, $0 is the whole line, which in this case is empty apart from possible white-space, and so has the numeric value 0.) At the end of the input the END pattern matches, so s is printed. However, since there may have been no lines of input at all, in which case no value has ever been assigned to s, it will by default be an empty string. Adding zero to a variable is an AWK idiom for coercing it from a string to a numeric value. (Concatenating an empty string is to coerce from a number to a string, e.g. s "". Note, there's no operator to concatenate strings, they're just placed adjacently.) With the coercion the program prints "0" on an empty input, without it, an empty line is printed. Match a range of input lines NR % 4 == 1, NR % 4 == 3