Chapter 11. A Flock of awks
In the previous four chapters, we have looked at POSIX awk, with only
occasional reference to actual awk implementations that you would run.
In this chapter, we
focus on the different versions of awk that are available,
what features they do or do not have, and how you can get them. First, we'll look at the original V7 version of awk.
The original awk lacks many of the features we've described, so this
section mostly describes what's not there.
Next, we'll look at the three versions whose source code is freely
available.
All of them have extensions to the POSIX standard.
Those that are common to all three versions are discussed
first.
Finally, we look at three commercial versions of awk.
In each of the sections that follow, we'll take a brief look at how
the original awk differs from POSIX awk.
Over the years, UNIX vendors have enhanced their versions of original
awk; you may need to write small test programs to see exactly what
features your old awk has or doesn't have.
11.1.5. Faking Dynamic Regular Expressions
The original awk made it difficult to
use patterns dynamically because they had to be fixed when
the script was interpreted.
You can get around the problem of not being able to
use a variable as a regular expression by
importing a shell variable inside an awk program. The value
of the shell variable will be interpreted by awk as a constant.
Here's an example:
$ cat awkro2
#!/bin/sh
# assign shell's $1 to awk search variable
search=$1
awk '$1 ~ /'"$search"'/' acronyms
The first line of the script makes the
variable assignment before awk is invoked.
To get the shell to expand the variable inside the awk procedure,
we enclose it within single, then double, quotation
marks.[72]
Thus, awk never sees the shell variable and evaluates
it as a constant string.
Here's another version that makes use of the Bourne shell variable
substitution feature.
Using this feature gives us an easy way to specify a default value for the
variable if, for instance, the user does not supply a command-line
argument.
search=$1
awk '$1 ~ /'"${search:-.*}"'/' acronyms
The expression "${search:-.*}" tells the shell to use the value
of search if it is defined; if not, use ".*" as the value.
Here, ".*" is regular-expression syntax specifying any string of
characters; therefore, all entries are printed if no entry
is supplied on the command line.
Because the whole thing is inside double quotes, the shell does not
perform a wildcard expansion on ".*".
11.1.6. Control Flow
In POSIX awk, if a program has just a BEGIN procedure, and
nothing else, awk will exit after executing that procedure.
The original awk is different; it will execute the BEGIN procedure
and then go on to process input, even if there are no pattern-action
statements.
You can force awk to exit by supplying /dev/null on the command
line as a data file argument, or by using exit.
In addition, the BEGIN and END procedures, if present, have
to be at the beginning and end of program, respectively.
Furthermore, you can only have one of each.
11.1.10. Functions
The original awk had only a limited number of built-in string functions.
(See Table 11.1 and
Table 11.3.)
Table 11.1. Original awk's Built-In String Functions
Awk Function |
Description |
index(s,t) |
Returns position of substring t in string s or zero if not present. |
length(s) |
Returns length of string s or length of $0 if no string is supplied. |
split(s,a,sep) |
Parses string s into elements of array a using field separator sep; returns number of elements. If sep is not supplied, FS is used. Array splitting works the same way as field splitting. |
sprintf("fmt",expr) |
Uses printf format specification for expr. |
substr(s,p,n) |
Returns substring of string s at beginning position
p up to maximum length of n. If n isn't supplied, the rest of the string from p is used. |
Some built-in functions
can be classified as arithmetic functions.
Most of them take a numeric argument and return
a numeric value. Table 11.2 summarizes
these arithmetic functions.
Table 11.2. Original awk's Built-In Arithmetic Functions
Awk Function |
Description |
exp(x) |
Returns e to the power x. |
int(x) |
Returns truncated value of x. |
log(x) |
Returns natural logarithm (base-e) of x. |
sqrt(x) |
Returns square root of x. |
One of the nicest facilities in awk, the ability to define your own
functions, is also not available in original awk.
 |  |  | 10.9. Invoking awk Using the #! Syntax |  | 11.2. Freely Available awks |
Copyright © 2003 O'Reilly & Associates. All rights reserved.
|
|