Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [122]

By Root 815 0
consult any of the books on awk listed in the Chapter 16. If GNU gawk is installed on your system, then its manual should be available in the online info system.[1]

All Unix systems have at least one awk implementation. When the language was significantly extended in the mid-1980s, some vendors kept the old implementation as awk, and sometimes also as oawk, and then named the new one nawk. IBM AIX and Sun Solaris both continue that practice, but most others now provide only the new one. Solaris has a POSIX-compliant version in /usr/xpg4/bin/awk. In this book, we consider only the extended language and refer to it as awk, even though you might have to use nawk, gawk, or mawk on your system.

We must confess here to a strong bias about awk. We like it. A lot. We have implemented, maintained, ported, written about, and used the language for many years. Even though many awk programs are short, some of our larger awk programs are thousands of lines long. The simplicity and power of awk often make it just the right tool for the job, and we seldom encounter a text processing task in which we need a feature that is not already in the language, or cannot be readily implemented. When we have on occasion rewritten an awk program in a conventional programming language like C or C++, the result was usually much longer, and much harder to debug, even if it did run somewhat faster.

Unlike most other scripting languages, awk enjoys multiple implementations, a healthy situation that encourages adherence to a common language base and that permits users to switch freely from one to another. Also, unlike other scripting languages, awk is part of POSIX, and there are implementations for non-Unix operating systems.

If your local version of awk is substandard, get one of the free implementations listed in Table 9-1. All of these programs are very portable and easy to install. gawk has served as a testbed for several interesting new built-in functions and language features, including network I/O, and also for profiling, internationalization, and portability checking.

Table 9-1. Freely available awk versions

Program

Location

Bell Labs awk

http://cm.bell-labs.com/who/bwk/awk.tar.gz

gawk

ftp://ftp.gnu.org/gnu/gawk/

mawk

ftp://ftp.whidbey.net/pub/brennan/mawk-1.3.3.tar.gz

awka

http://awka.sourceforge.net/ (awk-to-C translator)

The awk Command Line

An awk invocation can define variables, supply the program, and name the input files:

awk [ -F fs ] [ -v var=value ... ] 'program' [ -- ] \

[ var=value ... ] [ file(s) ]

awk [ -F fs ] [ -v var=value ... ] -f programfile [ -- ] \

[ var=value ... ] [ file(s) ]

Short programs are usually provided directly on the command line, whereas longer ones are relegated to files selected by the -f option. That option may be repeated, in which case the complete program is the concatenation of the specified program files. This is a convenient way to include libraries of shared awk code. Another approach to library inclusion is to use the igawk program, which is part of the gawk distribution. Options must precede filenames and ordinary var=value assignments.

If no filenames are specified on the command line, awk reads from standard input.

The — option is special: it indicates that there are no further command-line options for awk itself. Any following options are then available to your program.

The -F option redefines the default field separator, and it is conventional to make it the first command-line option. Its fs argument is a regular expression that immediately follows the -F, or is supplied as the next argument. The field separator can also be set with an assignment to the built-in variable FS (see Table 9-2 in Section 9.3.4, later in this chapter):

awk -F '\t' '{ ... }' files FS="[\f\v]" files

Here, the value set with the -F option applies to the first group of files, and the value assigned to FS applies to the second group.

Initializations with -v options must precede any program given directly on the command line; they take effect before the program

Return Main Page Previous Page Next Page

®Online Book Reader