In This Chapter
An Introduction to awk
Simple awk Scripting
Complex Applications in awk
Conditional and Looping Constructs in awk
Awk Built-in Functions
The awk programming language is a scripting language that can be used for very simple single-line applications to large applications. Awk is a general-purpose language, but it excels at its original task of text processing. In this chapter, we ll take a tour of the awk programming language, illustrating by numerous examples how it can be used by the developer. Awk takes over where sed left off, but each has its own individual strengths.
The first version of awk was first released in 1977 by Alfred Aho, Peter Weinberger, and Brian Kernighan (its name is the combination of the first letter of the authors last names ). Its first integration was into version 7 AT&T UNIX (as all three worked for Bell Labs at the time). Awk has gone through a variety of changes in its life. Its syntax and notation borrow both from shell scripting languages and also C.
The higher-level structure of awk programs is conceptually very simple (see Figure 22.1).
The structure of an awk program can be split into three sections. The BEGIN section is performed before the first line is read from the input file(s), and the END section is performed after the last line is processed from the input file(s). Between the optional BEGIN and END sections is the awk pattern/action section. For each input file specified, each of the patterns is compared in order, and if a pattern matches, then its associated action is performed.
We ll now split our discussion of awk into two sections. In the first, we ll look at simple awk programs that can be specified on the command line, and then we ll look at building more complex awk programs for scripting.