Conditional Statements
IF Statement
The syntax of the IF conditional statement is as follows:
if (condition)
action
You can also use curly braces to execute a group of actions:
if (condition)
{
action-1
action-1
.
.
action-n
}
The following example is used to determine whether a number is odd or even:
$ awk 'BEGIN {num = 10; if (num % 2 == 0) printf "%d 是偶数\n", num }'
The output result is:
10 is even
IF - ELSE Statement
The syntax of the IF - ELSE conditional statement is as follows:
if (condition)
action-1
else
action-2
When the condition condition is true, execute action-1; otherwise, execute action-2.
$ awk 'BEGIN {
num = 11;
if (num % 2 == 0) printf "%d 是偶数\n", num;
else printf "%d 是奇数\n", num
}'
The output result is:
11 是奇数
IF - ELSE - IF
We can create multiple IF - ELSE statements to implement the evaluation of multiple conditions:
$ awk 'BEGIN {
a=30;
if (a==10)
print "a = 10";
else if (a == 20)
print "a = 20";
else if (a == 30)
print "a = 30";
}'
The output result is:
a = 30
Loops
For
The syntax of the For loop is as follows:
for (initialisation; condition; increment/decrement)
action
The for statement first performs the initialization action, then checks the condition. If the condition is true, it executes the action, then performs the increment or decrement operation. As long as the condition is true, the loop will continue to execute. At the end of each loop, a condition check is performed; if the condition is false, the loop ends.
The following example uses a For loop to output numbers 1 to 5:
$ awk 'BEGIN { for (i = 1; i <= 5; ++i) print i }'
The output result is:
1 2 3 4 5
While
The syntax of the While loop is as follows:
while (condition)
action
The While loop first checks whether the condition is true. If the condition is true, it executes the action. This process repeats until the condition becomes false.
The following is an example of using a While loop to output numbers 1 to 5:
$ awk 'BEGIN {i = 1; while (i < 6) { print i; ++i } }'
The output result is:
1 2 3 4 5
Break
break is used to end the loop:
In the example below, when the calculated sum is greater than 50, break is used to end the loop:
$ awk 'BEGIN {
sum = 0; for (i = 0; i < 20; ++i) {
sum += i; if (sum > 50) break; else print "Sum =", sum
}
}'
The output result is:
Sum = 0 Sum = 1 Sum = 3 Sum = 6 Sum = 10 Sum = 15 Sum = 21 Sum = 28 Sum = 36 Sum = 45
Continue
The Continue statement is used to end the current iteration inside the loop body, thereby directly proceeding to the next loop iteration.
The following example outputs even numbers between 1 and 20:
$ awk 'BEGIN {for (i = 1; i <= 20; ++i) {if (i % 2 == 1) continue ; else print i} }'
The output result is:
2 4 6 8 10 12 14 16 18 20
Exit
Exit is used to end the execution of the script program.
This function accepts an integer as a parameter representing the exit status of the AWK process. If this parameter is not provided, its default status is 0.
In the following example, the AWK program ends when the sum is greater than 50.
$ awk 'BEGIN {
sum = 0; for (i = 0; i < 20; ++i) {
sum += i; if (sum > 50) exit(10); else print "Sum =", sum
}
}'
The output result is:
Sum = 0 Sum = 1 Sum = 3 Sum = 6 Sum = 10 Sum = 15 Sum = 21 Sum = 28 Sum = 36 Sum = 45
Let's check the return status after the script is executed:
$ echo $?
Executing the above command can yield the following result:
10