Comparison Operators Say that you have to flag

Comparison Operators Say that you have to flag all the fruit whose quantity is less than 75 for reorder by appending the string REORDER to the end of their line. In this case you have to check whether the third field, which holds the quantity, is less than or equal to 75. To solve this problem, you need to use a comparison operator. In awk, comparison operators compare the values of numbers and strings. Their behavior is similar to operators found in the C language or the shell. When you use a comparison operator, the syntax of an awk command changes to the following: expression { actions; } Here expression is constructed using one of the comparison operators given in Table 17.1 Table 17.1 Comparison Operators in awk Operator Description < Less than > Greater than <= Less than or equal to >= Greater than or equal to == Equal to != Not equal to value ~ / pattern/ True if value matches pattern value !~ / pattern/ True if value does not match pattern You can solve your problem using the following script: #!/bin/sh awk ‘ $3 <= 75 { printf "%st%sn",$0,"REORDER" ; } $3 > 75 { print $0 ; } ‘ fruit_prices.txt Here you check to see whether the third field contains a value less than or equal to 75. If it does, you print out the input line followed by the string REORDER. Next you check to see whether the third field contains a value greater than 75 and, if it does, you print the input line unchanged. The output from this scripts looks like the following: Fruit Price/lbs Quantity Banana $0.89 100 Peach $0.79 65 REORDER Kiwi $1.50 22 REORDER
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

Leave a Reply