AWK - Structure of AWK Programs

Structure of AWK Programs

"AWK is a language for processing text files. A file is treated as a sequence of records, and by default each line is a record. Each line is broken up into a sequence of fields, so we can think of the first word in a line as the first field, the second word as the second field, and so on. An AWK program is of a sequence of pattern-action statements. AWK reads the input a line at a time. A line is scanned for each pattern in the program, and for each pattern that matches, the associated action is executed." - Alfred V. Aho

An AWK program is a series of pattern action pairs, written as:

''condition'' { ''action'' }

where condition is typically an expression and action is a series of commands. The input is split into records, where by default records are separated by newline characters so that the input is split into lines. The program tests each record against each of the conditions in turn, and executes the action for each expression that is true. Either the condition or the action may be omitted. The condition defaults to matching every record. The default action is to print the record.

In addition to a simple AWK expression, such as foo == 1 or /^foo/, the condition can be BEGIN or END causing the action to be executed before or after all records have been read, or pattern1, pattern2 which matches the range of records starting with a record that matches pattern1 up to and including the record that matches pattern2 before again trying to match against pattern1 on future lines.

In addition to normal arithmetic and logical operators, AWK expressions include the tilde operator, ~, which matches a regular expression against a string. As handy syntactic sugar, /regexp/ without using the tilde operator matches against the current record.

Read more about this topic:  AWK

Famous quotes containing the words structure of, structure and/or programs:

    A special feature of the structure of our book is the monstrous but perfectly organic part that eavesdropping plays in it.
    Vladimir Nabokov (1899–1977)

    The syntactic component of a grammar must specify, for each sentence, a deep structure that determines its semantic interpretation and a surface structure that determines its phonetic interpretation.
    Noam Chomsky (b. 1928)

    Although good early childhood programs can benefit all children, they are not a quick fix for all of society’s ills—from crime in the streets to adolescent pregnancy, from school failure to unemployment. We must emphasize that good quality early childhood programs can help change the social and educational outcomes for many children, but they are not a panacea; they cannot ameliorate the effects of all harmful social and psychological environments.
    Barbara Bowman (20th century)