Original 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.

Escape Sequences

The original V7 awk only had “\t”, “\n”, “\"”, and, of course, “\\”. Most UNIX vendors have added some or all of “\b” and “\r” and “\f”.

Exponentiation

Exponentiation (using the ^, ^=, **, and **= operators) is not in old awk.

The C Conditional Expression

The three-argument conditional expression found in C, "expr1 ? expr2 : expr3" is not in old awk. You must resort to a plain old if-else statement.

Variables as Boolean Patterns

You cannot use the value of a variable as a Boolean pattern.

flag { print "..." }

You must instead use a comparison expression.

flag != 0 { print "..." }

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 ...

Get sed & awk, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.