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:

    In the extent and proper structure of the Union, therefore, we behold a republican remedy for the diseases most incident to republican government.
    James Madison (1751–1836)

    The question is still asked of women: “How do you propose to answer the need for child care?” That is an obvious attempt to structure conflict in the old terms. The questions are rather: “If we as a human community want children, how does the total society propose to provide for them?”
    Jean Baker Miller (20th century)

    Short of a wholesale reform of college athletics—a complete breakdown of the whole system that is now focused on money and power—the women’s programs are just as doomed as the men’s are to move further and further away from the academic mission of their colleges.... We have to decide if that’s the kind of success for women’s sports that we want.
    Christine H. B. Grant, U.S. university athletic director. As quoted in the Chronicle of Higher Education, p. A42 (May 12, 1993)