Contents | < Browse | Browse >
A More Complex Example
======================

   Here is an example to give you an idea of what typical `awk'
programs do.  This example shows how `awk' can be used to summarize,
select, and rearrange the output of another utility.  It uses features
that haven't been covered yet, so don't worry if you don't understand
all the details.

     ls -l | awk '$5 == "Nov" { sum += $4 }
                  END { print sum }'

   This command prints the total number of bytes in all the files in the
current directory that were last modified in November (of any year).
(In the C shell you would need to type a semicolon and then a backslash
at the end of the first line; in a POSIX-compliant shell, such as the
Bourne shell or the Bourne-Again shell, you can type the example as
shown.)

   The `ls -l' part of this example is a command that gives you a
listing of the files in a directory, including file size and date.  Its
output looks like this:

     -rw-r--r--  1 close        1933 Nov  7 13:05 Makefile
     -rw-r--r--  1 close       10809 Nov  7 13:03 gawk.h
     -rw-r--r--  1 close         983 Apr 13 12:14 gawk.tab.h
     -rw-r--r--  1 close       31869 Jun 15 12:20 gawk.y
     -rw-r--r--  1 close       22414 Nov  7 13:03 gawk1.c
     -rw-r--r--  1 close       37455 Nov  7 13:03 gawk2.c
     -rw-r--r--  1 close       27511 Dec  9 13:07 gawk3.c
     -rw-r--r--  1 close        7989 Nov  7 13:03 gawk4.c

The first field contains read-write permissions, the second field
contains the number of links to the file, and the third field
identifies the owner of the file.  The fourth field contains the size
of the file in bytes.  The fifth, sixth, and seventh fields contain the
month, day, and time, respectively, that the file was last modified.
Finally, the eighth field contains the name of the file.

   The `$5 == "Nov"' in our `awk' program is an expression that tests
whether the fifth field of the output from `ls -l' matches the string
`Nov'.  Each time a line has the string `Nov' in its fifth field, the
action `{ sum += $4 }' is performed.  This adds the fourth field (the
file size) to the variable `sum'.  As a result, when `awk' has finished
reading all the input lines, `sum' is the sum of the sizes of files
whose lines matched the pattern.  (This works because `awk' variables
are automatically initialized to zero.)

   After the last line of output from `ls' has been processed, the
`END' rule is executed, and the value of `sum' is printed.  In this
example, the value of `sum' would be 80600.

   These more advanced `awk' techniques are covered in later sections
(Actions).  Before you can move on to more
advanced `awk' programming, you have to know how `awk' interprets your
input and displays your output.  By manipulating fields and using
`print' statements, you can produce some very useful and spectacular
looking reports.