-->
Previous Table of Contents Next


Simple Patterns

As you might have figured out, gawk numbers all of the fields in a record. The first field is $1, the second is $2, and so on. The entire record is called $0. As a short form, gawk allows you to ignore the $0 in simple commands, so each of the following instructions results in the same output (the latter one because no action causes the entire line to be printed):


gawk ’/tparker/{print $0}’ /etc/passwd



gawk ’/tparker/{print}’ /etc/passwd



gawk ’/tparker/’ /etcpasswd

Suppose you want to do more than match a simple character string. The gawk language has many powerful features, but we’ll introduce just a few. We can, for example, make a comparison of a field with a value:


gawk ’$2 == “foo” {print $3}’ testfile

The preceding command instructs gawk to compare the second string ($2) of each record in testfile and check to see if it is equal to the string foo. If it is, gawk prints the third column ($3).

This command demonstrates a few important points. First, there are no slashes around the pattern because we are not matching a pattern but are evaluating something. Slashes are used only for character matches. Second, the == sign means “is equal to.” We must use two equal signs, because the single equal sign is used for assignment of values, as you will see shortly. Finally, we put double quotation marks around foo because we want gawk to interpret it literally. Only strings of characters that are to be literally interpreted must be quoted in this manner.


Note:  
Don’t confuse the quotation marks used for literal characters with those used to surround the pattern-action pair on the command line. If you use the same quotation marks for both, gawk is unable to process the command properly.

Comparisons and Arithmetic

An essential component of any programming language is the ability to compare two strings or numbers and evaluate whether they are equal or different. The gawk program has several comparisons, including ==, which you just saw in an example. Table 25.1 shows the important comparisons.

Table 25.1. The important comparisons.

Comparison Description

== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

These are probably familiar to you from arithmetic and other programming languages you may have seen. From this, you can surmise that the following command will display every line in testfile in which the value in the fourth column is greater than 100:


gawk ’$4 > 100’ testfile

All of the normal arithmetic commands are available, including add, subtract, multiply, and divide. There are also more advanced functions such as exponentials and remainders (also called moduli). Table 25.2 shows the basic arithmetic operations that gawk supports.

Table 25.2. Basic arithmetic operators.

Operator Description Example

+ Addition 2+6
- Subtraction 6-3
* Multiplication 2*5
/ Division 8/4
^ Exponentiation 3^2 (=9)
% Remainder 9%4 (=1)

You can combine column numbers and math, too:


{print $3/2}

This action divides the number in the third column by 2.

There is also a set of arithmetic functions for trigonometry and generating random numbers (see Table 25.3).

Table 25.3. Random-number and trigonometric functions.

Function Description

sqrt(x) Square root of x
sin(x) Sine of x (in radians)
cos(x) Cosine of x (in radians)
atan2(x,y) Arctangent of x/y
log(x) Natural logarithm of x
exp(x) The constant e to the power x
int(x) Integer part of x
rand() Random number between 0 and 1
srand(x) Set x as seed for rand()

The order of operations is important to gawk, as it is to regular arithmetic. The rules gawk follows are the same as with arithmetic: All multiplications, divisions, and remainders are performed before additions and subtractions:


{print $1+$2*$3}

The preceding command multiplies column two by column three and then adds the result to column one. If you wanted to force the addition first, use parentheses:


{print ($1+$2)*$3}

Because these are the same rules you have known about since grade school, they should not cause you any confusion. Remember, if in doubt, put parentheses in the proper places to force the operations.


Previous Table of Contents Next