Comparison expressions match lines where the action is performed if the condition is true. These expressions use relational operators and are used to compare numbers or strings. Table 6.1 provides a list of the relational operators. The value of the expression is 1 if the expression evaluates true, and 0 if false.
(The Database) % cat employees Tom Jones 4423 5/12/66 543354 Mary Adams 5346 11/4/63 28765 Sally Chang 1654 7/22/54 650000 Billy Black 1683 9/23/44 336500 (The Command Line) 1 % awk'$3 == 5346' employees Mary Adams 5346 11/4/63 28765 2 % awk'$3 > 5000{print $1} 'employees Mary 3 % awk '$2 ~ /Adam/ ' employees Mary Adams 5346 11/4/63 28765 4 % awk '$2 !~ /Adam/ ' employees Tom Jones 4423 5/12/66 543354 Sally Chang 1654 7/22/54 650000 Billy Black 1683 9/23/44 336500 |
Explanation
If the third field is equal to 5346, the condition is true and awk will perform the default action—print the line. When an if condition is implied, it is a conditional pattern test.
If the third field is greater than 5000, awk prints the first field.
If the second field does not match the regular expression Adam, the record is printed. If an expression is a numeric value and is being compared to a string value with an operator that requires a numeric comparison, the string value will be converted to a numeric value. If the operator requires a string value, the numeric value will be converted to a string value.
A conditional expression uses two symbols, the question mark and the colon, to evaluate expressions. It is really just a short way to achieve the same result as doing an if/else statement. The general format is:
Format
conditional expression1 ? expression2 : expression3
This produces the same result as the if/else shown here. (A complete discussion of the if/else construct is given later.)
{ if (expression1) expression2 else expression3 }
% awk '{max=($1 > $2) ? $1 : $2; print max}' filename
|
Explanation
If the first field is greater than the second field, the value of the expression after the question mark is assigned to max; otherwise the value of the expression after the colon is assigned to max.
This is comparable to:
if ($1 > $2 ) max=$1 else max=$2
Computation can be performed within patterns. Awk performs all arithmetic in floating point. The arithmetic operators are provided in Table 6.2.
% awk '$3 * $4 > 500' filename
|
Explanation
Awk will multiply the third field ($3) by the fourth field ($4), and if the result is greater than 500, it will display those lines. (Filename is assumed to be a file containing the input.)
Operator | Meaning | Example |
---|---|---|
+ | Add | x + y |
- | Subtract | x - y |
* | Multiply | x * y |
/ | Divide | x / y |
% | Modulus | x % y |
^ | Exponentiation | x ^ y |
Compound patterns are expressions that combine patterns with logical operators (see Table 6.3). An expression is evaluated from left to right.
Operator | Meaning | Example |
---|---|---|
&& | Logical AND | a && b |
|| | Logical OR | a || b |
! | NOT | ! a |
% awk '$2 > 5 && $2 <= 15' filename
|
Explanation
Awk will display those lines that match both conditions; that is, where the second field ($2) is greater than 5 AND the second field ($2) is also less than or equal to 15. With the && operator, BOTH conditions must be true. (Filename is assumed to be a file containing the input.)
% awk '$3 == 100 || $4 > 50' filename
|
Explanation
Awk will display those lines that match one of the conditions; that is, where the third field is equal to 100 OR the fourth field is greater than 50. With the || operator, only one of the conditions must be true. (Filename is assumed to be a file containing the input.)
% awk '!($2 < 100 && $3 < 20)' filename
|
Explanation
If both conditions are true, awk will negate the expression and display those lines—so the lines displayed will have one or both conditions false. The unary ! operator negates the result of the condition so that if the expression yields a true condition, the not will make it false, and vice versa. (Filename is assumed to be a file containing the input.)
Range patterns match from the first occurrence of one pattern to the first occurrence of the second pattern, then match for the next occurrence of the first pattern to the next occurrence of the second pattern, etc. If the first pattern is matched and the second pattern is not found, awk will display all lines to the end of the file.
% awk '/Tom/,/Suzanne/' filename
|
Explanation
Awk will display all lines, inclusive, that range between the first occurrence of Tom and the first occurrence of Suzanne. If Suzanne is not found, awk will continue processing lines until the end of file. If, after the range between Tom and Suzanne is printed, Tom appears again, awk will start displaying lines until another Suzanne is found or the file ends.
Using the awk commands discussed so far, the password-checking program from the book The AWK Programming Language [1] illustrates how the data in a file can be validated.
[1] Aho, Weinberger, and Kernighan, The Awk Programming Language. Addison Wesley, 1988.
(The Password Database) 1 % cat /etc/passwd tooth:pwHfudo.eC9sM:476:40:Contract Admin.:/home/rickenbacker/tooth:/bin/csh lisam:9JY7OuS2f3lHY:4467:40:Lisa M. Spencer:/home/fortune1/lisam:/bin/csh goode:v7Ww.nWJCeSIQ:32555:60:Goodwill Guest User:/usr/goodwill:/bin/csh bonzo:eTZbu6M2jM7VA:5101:911: SSTOOL Log account :/home/sun4/bonzo:/bin/csh info:mKZsrioPtW9hA:611:41:Terri Stern:/home/chewie/info:/bin/csh cnc:IN1IVqVj1bVv2:10209:41:Charles Carnell:/home/christine/cnc:/bin/csh bee:*:347:40:Contract Temp.:/home/chanel5/bee:/bin/csh friedman:oyuIiKoFTV0TE:3561:50:Jay Friedman:/home/ibanez/friedman:/bin/csh chambers:Rw7R1k77yUY4.:592:40:Carol Chambers:/usr/callisto2/chambers:/bin/csh gregc:nkLulOg:7777:30:Greg Champlin FE Chicago ramona:gbDQLdDBeRc46:16660:68:RamonaLeininge MWA CustomerService Rep:/home/forsh: (The Awk Commands) 2 % cat /etc/passwd | awk –F: ' 3 NF != 7{ 4 printf("line %d, does not have 7 fields: %s ",NR,$0)} 5 $1 !~ /[A–Za–z0–9]/{printf("line %d, nonalphanumeric user id: %s ",NR,$0)} 6 $2 == "*" {printf("line %d, no password: %s ",NR,$0)}' (The Output) line 7, no password: bee:*:347:40:Contract Temp.:/home/chanel5/bee:/bin/csh line 10, does not have 7 fields: gregc:nk2EYi7kLulOg:7777:30:Greg Champlin FE Chicago line 11, does not have 7 fields: ramona:gbDQLdDBeRc46:16660:68:Ramona Leininger MWA Customer Service Rep:/home/forsh: |
Explanation
The contents of the /etc/passwd file are displayed.
The cat program sends its output to awk. Awk's field separator is a colon.
If the number of fields (NF) is not equal to 7, the following action block is executed.
The printf function prints the string "line <number>, does not have 7 fields" followed by the number of the current record (NR) and the record itself ($0).
If the first field ($1) does not contain any alphanumeric characters, the printf function prints the string "nonalphanumeric user id," followed by the number of the record and the record.
If the second field ($2) equals an asterisk, the string "no passwd" is printed, followed by the number of the record and the record itself.
18.221.136.142