Awk Condition Statement

Source: Internet
Author: User

Awk Condition Statement

A condition statement is used for a test before an operation is executed. In the previous chapter, we saw some examples of pattern matching rules. A pattern matching rule is essentially a conditional expression that affects the input loop. In this part, we mainly discuss the condition statements used in the action. The Condition Statement starts with if and calculates the expression placed in parentheses. Syntax:

If (expression)
Action1
[Else
Action2]

If the expression value is true (non-zero or non-empty), action1 is executed. When an else statement exists, if the value of the conditional expression is false (zero or empty), action2. A conditional expression may contain Arithmetic Operators, Relational operators, or boolean operators, which have been discussed in awk scripts. Perhaps the simplest condition expression is to test whether a variable is non-zero.

If (x) print x

If x is zero, the print statement is not executed. If x is a non-zero value, the value of x is printed. You can also test whether x is equal to another value:

If (x = y) print x

Note that "=" is a relational operator and "=" is a value assignment operator. We can also use the pattern matching operator "~ "To test whether x matches a pattern:

If (x ~ /[YY] (es )? /) Print x

Below are some supplementary syntaxes. If the operation is composed of multiple statements, enclose the operation with a pair of braces.

If (expression)
Statement1
Statement2
}

Awk has no special requirements on the positions of braces and statements (unlike sed ). The left braces are placed behind the condition expression. They can be in either one line or the next line with the condition expression. The first statement can be followed by the Left braces or start from the next line. The right braces are placed behind the best statement. It can be in the same line as the last statement or the next line. Spaces or tabs are allowed before and after braces. Although statement indentation is not required, this can improve readability.

The linefeed following the right braces and else is optional.

If (expression) action1
[Else action2]

If you add a semicolon to the end of action1, the line feed after action1 is optional.

If (expression) action1; [else action2]

If multiple statements are separated by semicolons on the same line, braces are also required.

In the previous article, we saw a script used to calculate the average score of students. We can use one condition to determine whether a student has passed.

If (avg> = 65)
Grade = "Pass"
Else
Grade = "Fail"

The value assigned to grade depends on whether the calculation result of the expression "avg> = 65" is true or false.

You can use multiple condition statements to test whether a condition is true. For example, a student's score may be represented in alphabetical order, rather than passing or failing. The following uses a condition structure to specify a letter score based on the average score of students:

If (avg> = 90) grade = ""
Else if (avg> = 80) grade = "B"
Else if (avg> = 70) grade = "C"
Else if (avg> = 60) grade = "D"
Else grade = "F"

One important thing to note is that such continuous conditions stop evaluate only when the calculation result of a conditional expression is true. In this case, other conditions are skipped. If no conditional expression returns true, the last else part is executed to run the default operation. In this case, the grade value is assigned "F ".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.