This is a scripting language developed under the UNIX operating system (see scripting languages) by Alfred V. Aho, Brian W. Kernighan, and Peter J. Weinberger in 1977. (The name is an acronym from their last initials.) The lan-guage builds upon many of the pattern matching utilities of the operating system and is designed primarily for the extraction and reporting of data from files. A number of variants of awk have been developed for other operating systems such as DOS.
As with other scripting languages, an awk program con-sists of a series of commands read from a file by the awk interpreter. For example the following UNIX command line:
awk -f MyProgram > Report
reads awk statements from the file MyProgram into the awk interpreter and sends the program’s output to the file Report.
Language Features
An awk statement consists of a pattern to match and an action to be taken with the result (although the pattern can be omitted if not needed). Here are some examples:
{print $1} # prints the first field of every
# line of input (since no pattern
# is specified)
/debit/ {print $2} # print the second field of
# every line that contains the
# word “debit”
if ( Code == 2 ) # if Code equals 2, print $3 # print third field
# of each line
Pattern matching uses a variety of regular expressions famil-iar to UNIX users. Actions can be specified using a limited but adequate assortment of control structures similar to those found in C. There are also built-in variables (including counters for the number of lines and fields), arithmetic func-tions, useful string functions for extracting text from fields, and arithmetic and relational operators. Formatting of out-put can be accomplished through the versatile (but some-what cryptic) print function familiar to C programmers.
Awk became popular for extracting reports from data files and simple databases on UNIX systems. For more sophisticated applications it has been supplanted by Perl,
which offers a larger repertoire of database-oriented fea-tures (see Perl).
As with other scripting languages, an awk program con-sists of a series of commands read from a file by the awk interpreter. For example the following UNIX command line:
awk -f MyProgram > Report
reads awk statements from the file MyProgram into the awk interpreter and sends the program’s output to the file Report.
Language Features
An awk statement consists of a pattern to match and an action to be taken with the result (although the pattern can be omitted if not needed). Here are some examples:
{print $1} # prints the first field of every
# line of input (since no pattern
# is specified)
/debit/ {print $2} # print the second field of
# every line that contains the
# word “debit”
if ( Code == 2 ) # if Code equals 2, print $3 # print third field
# of each line
Pattern matching uses a variety of regular expressions famil-iar to UNIX users. Actions can be specified using a limited but adequate assortment of control structures similar to those found in C. There are also built-in variables (including counters for the number of lines and fields), arithmetic func-tions, useful string functions for extracting text from fields, and arithmetic and relational operators. Formatting of out-put can be accomplished through the versatile (but some-what cryptic) print function familiar to C programmers.
Awk became popular for extracting reports from data files and simple databases on UNIX systems. For more sophisticated applications it has been supplanted by Perl,
which offers a larger repertoire of database-oriented fea-tures (see Perl).
No comments:
Post a Comment