6.11. Comparison Expressions

 <  Day Day Up  >  

Comparison 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 Operators

Table 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 ' == 5346' employees   Mary Adams        5346    11/4/63    28765  2   %  awk ' > 5000{print } ' employees   Mary  3   %  awk ' ~ /Adam/ ' employees   Mary Adams    5346    11/4/63       28765  4   %  awk ' !~ /Adam/ ' employees   Tom Jones         4423    5/12/66    543354   Sally Chang    1654    7/22/54    650000   Billy Black    1683    9/23/44    336500  

Table 6.8. Relational Operators

Operator

Meaning

Example

<

Less than

x < y

<=

Less than or equal to

x <= y

==

Equal to

x == y

!=

Not equal to

x != y

>=

Greater than or equal to

x >= y

>

Greater than

x > y

~

Matched by regular expression

x ~ /y/

!~

Not matched by regular expression

x !~ /y/


EXPLANATION

  1. If the third field is exactly 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.

  2. If the third field is greater than 5000 , awk prints the first field.

  3. If the second field matches or contains the regular expression Adam , the record is printed.

  4. If the second field does not match or does not contain the regular expression Adam , the record is printed. If an expression is a numeric value and is being compared with 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.

6.11.2 Conditional Expressions

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 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=(>) ?  : ; 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 (>)         max= else         max= 

6.11.3 Computation

Computation 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 ' *  > 500' filename  

Table 6.9. Arithmetic Operators

Operator

Meaning

Example

+

Add

x + y

Subtract

x - y

*

Multiply

x * y

/

Divide

x / y

%

Modulus

x % y

^

Exponentiation

x ^ y


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 Patterns

Logical 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 ' > 5 &&  <= 15' filename  

Table 6.10. Logical Operators

Operator

Meaning

Example

&&

Logical AND

a && b

Logical OR

a b

!

NOT

! a


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 ' == 100   > 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 '!(< 100 &&  < 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 Patterns

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, 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 Program

Using 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 .

[3] Aho, Weinberger, and Kernighan, The Awk Programming Language (Boston: Addison-Wesley, 1988).

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, 
 (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:  
)} \
5 !~ /[AZaz09]/{printf("line %d, nonalphanumeric user id: %s\n",NR,
 (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:  
)} \
6 == "*" {printf("line %d, no password: %s\n",NR,
 (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:  
)} '
(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

  1. The contents of the /etc/passwd file are displayed.

  2. The cat program sends its output to awk . Awk 's field separator is a colon.

  3. If the number of fields ( NF ) is not equal to 7, the following action block is executed.

  4. 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 ).

  5. 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 itself.

  6. If the second field ( $2 ) equals an asterisk, the string no password is printed, followed by the number of the record and the record itself.

 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net