6.12. Review
The examples in this section use the following sample database, called datafile, repeated periodically for your convenience.
% cat datafile |
---|
northwest | NW | Joel Craig | 3.0 | .98 | 3 | 4 | western | WE | Sharon Kelly | 5.3 | .97 | 5 | 23 | southwest | SW | Chris Foster | 2.7 | .8 | 2 | 18 | southern | SO | May Chin | 5.1 | .95 | 4 | 15 | southeast | SE | Derek Johnson | 4.0 | .7 | 4 | 17 | eastern | EA | Susan Beal | 4.4 | .84 | 5 | 20 | northeast | NE | TJ Nichols | 5.1 | .94 | 3 | 13 | north | NO | Val Shultz | 4.5 | .89 | 5 | 9 | central | CT | Sheri Watson | 5.7 | .94 | 5 | 13 |
6.12.1 Equality Testing
Example 6.62.
% awk '$7 == 5' datafile
western WE Sharon Kelly 5.3 .97 5 23
eastern EA Susan Beal 4.4 .84 5 20
north NO Val Shultz 4.5 .89 5 9
central CT Sheri Watson 5.7 .94 5 13
EXPLANATION
If the seventh field ($7) is equal to the number 5, the record is printed.
Example 6.63.
% awk '$2 == "CT"{print $1, $2}' datafile
central CT
EXPLANATION
If the second field is equal to the string CT, fields one and two ($1, $2) are printed. Strings must be quoted.
Example 6.64.
% awk '$7 != 5' datafile
northwest NW Joel Craig 3.0 .98 3 4
southwest SW Chris Foster 2.7 .8 2 18
southern SO May Chin 5.1 .95 4 15
southeast SE Derek Johnson 4.0 .7 4 17
northeast NE TJ Nichols 5.1 .94 3 13
EXPLANATION
If the seventh field ($7) is not equal to the number 5, the record is printed.
6.12.2 Relational Operators
Example 6.65.
% awk '$7 < 5 {print $4, $7}' datafile
Craig 3
Foster 2
Chin 4
Johnson 4
Nichols 3
EXPLANATION
If the seventh field ($7) is less than 5, fields 4 and 7 are printed.
Example 6.66.
% awk '$6 > .9 {print $1, $6}' datafile
northwest .98
western .97
southern .95
northeast .94
central .94
EXPLANATION
If the sixth field ($6) is greater than .9, fields 1 and 6 are printed.
Example 6.67.
% awk '$8 <= 17 { print $8}' datafile
4
15
17
13
9
13
EXPLANATION
If the eighth field ($8) is less than or equal to 17, it is printed.
% cat datafile |
---|
northwest | NW | Joel Craig | 3.0 | .98 | 3 | 4 | western | WE | Sharon Kelly | 5.3 | .97 | 5 | 23 | southwest | SW | Chris Foster | 2.7 | .8 | 2 | 18 | southern | SO | May Chin | 5.1 | .95 | 4 | 15 | southeast | SE | Derek Johnson | 4.0 | .7 | 4 | 17 | eastern | EA | Susan Beal | 4.4 | .84 | 5 | 20 | northeast | NE | TJ Nichols | 5.1 | .94 | 3 | 13 | north | NO | Val Shultz | 4.5 | .89 | 5 | 9 | central | CT | Sheri Watson | 5.7 | .94 | 5 | 13 |
Example 6.68.
% awk '$8 >= 17 {print $8}' datafile
23
18
17
20
EXPLANATION
If the eighth field ($8) is greater than or equal to 17, the eighth field is printed.
6.12.3 Logical Operators (&&, ||)
Example 6.69.
% awk '$8 > 10 && $8 < 17' datafile
southern SO May Chin 5.1 .95 4 15
northeast NE TJ Nichols 5.1 .94 3 13
central CT Sheri Watson 5.7 .94 5 13
EXPLANATION
If the eighth field ($8) is greater than 10 AND less than 17, the record is printed. The record will be printed only if both expressions are true.
Example 6.70.
% awk '$2 == "NW" || $1 ~ /south/{print $1, $2}' datafile
northwest NW
southwest SW
southern SO
southeast SE
EXPLANATION
If the second field ($2) is equal to the string NW OR the first field ($1) contains the pattern south, the first and second fields ($1, $2) are printed. The record will be printed if only one of the expressions is true.
6.12.4 Logical NOT Operator (!)
Example 6.71.
% awk '!($8 == 13){print $8}' datafile
4
23
18
15
17
20
9
EXPLANATION
If the eighth field ($8) is equal to 13, the ! (NOT operator) NOTs the expression and prints the eighth field ($8). The ! is a unary negation operator.
6.12.5 Arithmetic Operators
Example 6.72.
% awk '/southern/{print $5 + 10}' datafile
15.1
EXPLANATION
If the record contains the regular expression southern, 10 is added to the value of the fifth field ($5) and printed. Note that the number prints in floating point.
Example 6.73.
% awk '/southern/{print $8 + 10}' datafile
25
EXPLANATION
If the record contains the regular expression southern, 10 is added to the value of the eighth field ($8) and printed. Note that the number prints in decimal.
% cat datafile |
---|
northwest | NW | Joel Craig | 3.0 | .98 | 3 | 4 | western | WE | Sharon Kelly | 5.3 | .97 | 5 | 23 | southwest | SW | Chris Foster | 2.7 | .8 | 2 | 18 | southern | SO | May Chin | 5.1 | .95 | 4 | 15 | southeast | SE | Derek Johnson | 4.0 | .7 | 4 | 17 | eastern | EA | Susan Beal | 4.4 | .84 | 5 | 20 | northeast | NE | TJ Nichols | 5.1 | .94 | 3 | 13 | north | NO | Val Shultz | 4.5 | .89 | 5 | 9 | central | CT | Sheri Watson | 5.7 | .94 | 5 | 13 |
Example 6.74.
% awk '/southern/{print $5 + 10.56}' datafile
15.66
EXPLANATION
If the record contains the regular expression southern, 10.56 is added to the value of the fifth field ($5) and printed.
Example 6.75.
% awk '/southern/{print $8 - 10}' datafile
5
EXPLANATION
If the record contains the regular expression southern, 10 is subtracted from the value of the eighth field ($8) and printed.
Example 6.76.
% awk '/southern/{print $8 / 2}' datafile
7.5
EXPLANATION
If the record contains the regular expression southern, the value of the eighth field ($8) is divided by 2 and printed.
Example 6.77.
% awk '/northeast/{print $8 / 3}' datafile
4.33333
EXPLANATION
If the record contains the regular expression northeast, the value of the eighth field ($8) is divided by 3 and printed. The precision is five places to the right of the decimal point.
Example 6.78.
% awk '/southern/{print $8 * 2}' datafile
30
EXPLANATION
If the record contains the regular expression southern, the eighth field ($8) is multiplied by 2 and printed.
Example 6.79.
% awk '/northeast/ {print $8 % 3}' datafile
1
EXPLANATION
If the record contains the regular expression northeast, the eighth field ($8) is divided by 3 and the remainder (modulus) is printed.
Example 6.80.
% awk '$3 ~ /^Susan/\
{print "Percentage: "$6 + .2 " Volume: " $8}' datafile
Percentage: 1.04 Volume: 20
EXPLANATION
If the third field ($3) begins with the regular expression Susan, the print function prints the result of the calculations and the strings in double quotes.
% cat datafile |
---|
northwest | NW | Joel Craig | 3.0 | .98 | 3 | 4 | western | WE | Sharon Kelly | 5.3 | .97 | 5 | 23 | southwest | SW | Chris Foster | 2.7 | .8 | 2 | 18 | southern | SO | May Chin | 5.1 | .95 | 4 | 15 | southeast | SE | Derek Johnson | 4.0 | .7 | 4 | 17 | eastern | EA | Susan Beal | 4.4 | .84 | 5 | 20 | northeast | NE | TJ Nichols | 5.1 | .94 | 3 | 13 | north | NO | Val Shultz | 4.5 | .89 | 5 | 9 | central | CT | Sheri Watson | 5.7 | .94 | 5 | 13 |
6.12.6 Range Operator
Example 6.81.
% awk '/^western/,/^eastern/' datafile
western WE Sharon Kelly 5.3 .97 5 23
southwest SW Chris Foster 2.7 .8 2 18
southern SO May Chin 5.1 .95 4 15
southeast SE Derek Johnson 4.0 .7 4 17
eastern EA Susan Beal 4.4 .84 5 20
EXPLANATION
All records within the range beginning with the regular expression western are printed until a record beginning with the expression eastern is found. Records will start being printed again if the pattern western is found and will continue to print until eastern or the end of the file is reached.
6.12.7 Conditional Operator
Example 6.82.
% awk '{print ($7 > 4 ? "high "$7 : "low "$7)}' datafile
low 3
high 5
low 2
low 4
low 4
high 5
low 3
high 5
high 5
EXPLANATION
If the seventh field ($7) is greater than 4, the print function gets the value of the expression after the question mark (the string high and the seventh field); else the print function gets the value of the expression after the colon (the string low and the value of the seventh field).
6.12.8 Assignment Operators
Example 6.83.
% awk '$3 == "Chris"{ $3 = "Christian"; print}' datafile
southwest SW Christian Foster 2.7 .8 2 18
EXPLANATION
If the third field ($3) is equal to the string Chris, the action is to assign Christian to the third field ($3) and print the record. The double equal tests its operands for equality, whereas the single equal is used for assignment.
Example 6.84.
% awk '/Derek/{$8 += 12; print $8}' datafile
29
EXPLANATION
If the regular expression Derek is found, 12 is added and assigned to (+=), the eighth field ($8), and that value is printed. Another way to write this is $8 = $8 + 12.
Example 6.85.
% awk '{$7 %= 3; print $7}' datafile
0
2
2
1
1
2
0
2
2
EXPLANATION
For each record, the seventh field ($7) is divided by 3, and the remainder of that division (modulus) is assigned to the seventh field and printed.
|