Chapter 22: Text Processing with awk


 Download CD Content

In This Chapter

  • An Introduction to awk

  • Simple awk Scripting

  • Complex Applications in awk

  • Conditional and Looping Constructs in awk

  • Awk Built-in Functions

Introduction

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.

Short History

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.

Awk Structure

The higher-level structure of awk programs is conceptually very simple (see Figure 22.1).

click to expand
Figure 22.1:    Structure of an awk program.

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.




GNU/Linux Application Programming
GNU/Linux Application Programming (Programming Series)
ISBN: 1584505680
EAN: 2147483647
Year: 2006
Pages: 203
Authors: M. Tim Jones

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net