< Day Day Up > |
6.11. Comparison ExpressionsComparison expressions match lines where the action is performed only if a certain condition is true. These expressions use relational operators and are used to compare numbers or strings. 6.11.1 Relational and Equality OperatorsTable 6.8 provides a list of the relational operators. The value of the expression is 1 if the expression evaluates true, and 0 if false. Example 6.54.(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
6.11.2 Conditional ExpressionsA 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 as follows: 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 } Example 6.55.
% 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 6.11.3 ComputationComputation can be performed within patterns. Awk (all versions) performs all arithmetic in floating point. The arithmetic operators are provided in Table 6.9. Example 6.56.
% 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.) 6.11.4 Logical Operators and Compound PatternsLogical operators test whether an expression or pattern is true or false. With the &&, logical AND operator, if both expressions are true, the entire expression is true. If one expression is false, the whole expression is false. With ||, the logical OR operator, only one expression or pattern must evaluate to true to make the whole expression true. If both expressions are false, the entire expression is false. Compound patterns are expressions that combine patterns with logical operators (see Table 6.10). An expression is evaluated from left to right. Example 6.57.
% 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.) Example 6.58.
% 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.) Example 6.59.
% 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.) 6.11.5 Range PatternsRange 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, and so on. If the first pattern is matched and the second pattern is not found, awk will display all lines to the end of the file. Example 6.60.
% 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 the 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. 6.11.6 A Data Validation ProgramUsing the awk commands discussed so far, the password-checking program from the book The AWK Programming Language[3] illustrates how the data in a file can be validated.
Example 6.61.(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\n",NR,$0)} \ 5 $1 !~ /[A–Za–z0–9]/{printf("line %d, nonalphanumeric user id: %s\n",NR,$0)} \ 6 $2 == "*" {printf("line %d, no password: %s\n",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
|
< Day Day Up > |