home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeBook TitleSearch this book

Chapter 11. The awk Programming Language

This chapter presents the following topics:

For more information, see sed & awk, listed in the Bibliography.

11.1. Conceptual Overview

awk is a pattern-matching program for processing files, especially when they are databases. The new version of awk, called nawk, provides additional capabilities.[10] Every modern Unix system comes with a version of new awk, and its use is recommended over old awk.

[10]It really isn't so new. The additional features were added in 1984, and it was first shipped with System V Release 3.1 in 1987. Nevertheless, the name was never changed on most systems.

Different systems vary in what the two versions are called. Some have oawk and awk, for the old and new versions, respectively. Others have awk and nawk. Still others only have awk, which is the new version. This example shows what happens if your awk is the old one:

$ awk 1 /dev/null
awk: syntax error near line 1
awk: bailing out near line 1

awk exits silently if it is the new version.

Source code for the latest version of awk, from Bell Labs, can be downloaded starting at Brian Kernighan's home page: http://cm.bell-labs.com/~bwk. Michael Brennan's mawk is available via anonymous FTP from ftp://ftp.whidbey.net/pub/brennan/mawk1.3.3.tar.gz. Finally, the Free Software Foundation has a version of awk called gawk, available from ftp://gnudist.gnu.org/gnu/gawk/gawk-3.0.4.tar.gz. All three programs implement “new” awk. Thus, references below such as “nawk only,” apply to all three. gawk has additional features.

With original awk, you can:

  • Think of a text file as made up of records and fields in a textual database.

  • Perform arithmetic and string operations.

  • Use programming constructs such as loops and conditionals.

  • Produce formatted reports.

With nawk, you can also:

  • Define your own functions.

  • Execute Unix commands from a script.

  • Process the results of Unix commands.

  • Process command-line arguments more gracefully.

  • Work more easily with multiple input streams.

  • Flush open output files and pipes (latest Bell Labs awk).

In addition, with GNU awk (gawk), you can:

  • Use regular expressions to separate records, as well as fields.

  • Skip to the start of the next file, not just the next record.

  • Perform more powerful string substitutions.

  • Retrieve and format system time values.

Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.