gawk: More Complex
1
1 1.5 A More Complex Example
1 ==========================
1
1 Now that we've mastered some simple tasks, let's look at what typical
1 'awk' programs do. This example shows how 'awk' can be used to
1 summarize, select, and rearrange the output of another utility. It uses
1 features that haven't been covered yet, so don't worry if you don't
1 understand all the details:
1
1 ls -l | awk '$6 == "Nov" { sum += $5 }
1 END { print sum }'
1
1 This command prints the total number of bytes in all the files in the
1 current directory that were last modified in November (of any year).
1 The 'ls -l' part of this example is a system command that gives you a
1 listing of the files in a directory, including each file's size and the
1 date the file was last modified. Its output looks like this:
1
1 -rw-r--r-- 1 arnold user 1933 Nov 7 13:05 Makefile
1 -rw-r--r-- 1 arnold user 10809 Nov 7 13:03 awk.h
1 -rw-r--r-- 1 arnold user 983 Apr 13 12:14 awk.tab.h
1 -rw-r--r-- 1 arnold user 31869 Jun 15 12:20 awkgram.y
1 -rw-r--r-- 1 arnold user 22414 Nov 7 13:03 awk1.c
1 -rw-r--r-- 1 arnold user 37455 Nov 7 13:03 awk2.c
1 -rw-r--r-- 1 arnold user 27511 Dec 9 13:07 awk3.c
1 -rw-r--r-- 1 arnold user 7989 Nov 7 13:03 awk4.c
1
1 The first field contains read-write permissions, the second field
1 contains the number of links to the file, and the third field identifies
1 the file's owner. The fourth field identifies the file's group. The
1 fifth field contains the file's size in bytes. The sixth, seventh, and
1 eighth fields contain the month, day, and time, respectively, that the
1 file was last modified. Finally, the ninth field contains the file
1 name.
1
1 The '$6 == "Nov"' in our 'awk' program is an expression that tests
1 whether the sixth field of the output from 'ls -l' matches the string
1 'Nov'. Each time a line has the string 'Nov' for its sixth field, 'awk'
1 performs the action 'sum += $5'. This adds the fifth field (the file's
1 size) to the variable 'sum'. As a result, when 'awk' has finished
1 reading all the input lines, 'sum' is the total of the sizes of the
1 files whose lines matched the pattern. (This works because 'awk'
1 variables are automatically initialized to zero.)
1
1 After the last line of output from 'ls' has been processed, the 'END'
1 rule executes and prints the value of 'sum'. In this example, the value
1 of 'sum' is 80600.
1
1 These more advanced 'awk' techniques are covered in later minor nodes
1 (⇒Action Overview). Before you can move on to more advanced
1 'awk' programming, you have to know how 'awk' interprets your input and
1 displays your output. By manipulating fields and using 'print'
1 statements, you can produce some very useful and impressive-looking
1 reports.
1