-->
Previous | Table of Contents | Next |
We could increase the sophistication of gawk searches in a number of ways. Firstly, we could incorporate the use of compound searches, which use three logical operators:
For example, lets say we wanted to know how many workers had a value in the fifth field that is greater than or equal to 10:
gilbert:/$ gawk '$5 >= 10 { print $0 } ' workers Geisha 280 555-4221 geisha 10 Tom 284 555-2121 spike 12
We can also combine tests, to print out, for example, all workers who have the fifth field less than 10 and the second field greater than 280:
gilbert:/$ gawk '$5 < 10 && $2 > 280 { print $0 } ' workers Eric 286 555-6674 erc 8
While these examples are obviously contrived, you can use gawk to help pull out all entries that share certain postal (ZIP) codes or all employees who have a salary in a certain range. Were just scratching the surface with gawk.
gawk can also be used to return entire sections of data, as long as you can specify patterns that begin and end the section. To return the records of Eric and Kevin and all between, use the following:
gilbert:/$ gawk '$1 ~ /Eric/,/Kevin/ {print $0}' workers Eric 286 555-6674 erc 8 Geisha 280 555-4221 geisha 10 Kevin 279 555-1112 kevin 2
If we dont want to print the whole record, we can print just a few of the fields, as in the following example, which prints out fields 2 and 1:
gilbert:/$ gawk '$1 ~ /Eric/,/Kevin/ {print $2, $1}' workers 286 Eric 280 Geisha 279 Kevin
As with other UNIX commands, gawk can be used in pipes, and its output can be directed to other files or directly to the printer. For example, if we were looking through a large file and expecting many matches to a particular string (such as salary ranges or employment starting dates), we might want to direct that output to a file or to a printer.
To use gawk with the Linux sort utility, we can sort the output of the last example:
gilbert:/$ gawk '$1 ~ /Eric/,/Kevin/ {print $2, $1}' workers | sort 279 Kevin 280 Geisha 286 Eric
(Please note that this is sorting on the leading number.)
Gawk also provides some summary abilities. The NR symbol in a gawk command returns the number of records, for example.
We can combine this with gawks ability to total fields in an gawk program.
Gawk Programs
Youre not limited to what fits on the command line with gawk. You can also store a series of gawk commands in a file and then use gawk to execute the file.
For example, we can store our simplest gawk command, {print}, in a separate file and use the following gawk command:
gilbert:/$ gawk -f gawk.1 workers Eric 286 555-6674 erc 8 Geisha 280 555-4221 geisha 10 Kevin 279 555-1112 kevin 2 Tom 284 555-2121 spike 12
In this case, were assuming that the file gawk.1 contains our very simple gawk program:
{ print }
You can combine this with the gawk BEGIN, END, and NR commands to make a more complex gawk program. When working with this, its good to remember that gawk applies each gawk command to every record, that is, every line of text, in the input file. A commandlike {print} says that each line in the input file should be printed.
The gawk BEGIN command lists what to do before reading each line of text. For example:
BEGIN { print "Workers for Spacely Sprockets"; print "" } { print }
Previous | Table of Contents | Next |