English: ## AWK Conditional Statements and Loops
Category Programming Techniques
Conditional Statements
IF Statement
The syntax of the IF statement is as follows:
if (condition)
action
You can also use curly braces to perform a group of actions:
if (condition)
{
action-1
action-2
.
.
action-n
}
The following example is used to determine whether the number is odd or even:
$ awk 'BEGIN {num = 10; if (num % 2 == 0) printf "%d is even\n", num }'
The output result is:
10 is even
IF - ELSE Statement
The syntax of the IF - ELSE statement is as follows:
if (condition)
action-1
else
action-2
When the condition in the condition statement is true, action-1 is executed, otherwise action-2 is executed.
$ awk 'BEGIN {
num = 11;
if (num % 2 == 0) printf "%d is even\n", num;
else printf "%d is odd\n", num
}'
The output result is:
11 is odd
IF - ELSE - IF
We can create multiple IF - ELSE statements to implement multiple condition judgments:
$ 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 (initialization; condition; increment/decrement)
action
The for statement first performs the initialization action (initialization), and then checks the condition (condition). If the condition is true, the action (action) is executed, followed by the increment (increment) or decrement (decrement) operation. As long as the condition is true, the loop will continue to execute. Each time the loop ends, it will check the condition, and if the condition is false, the loop will end.
The following example uses a For loop to output the 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 condition is true. If the condition is true, the action action is executed. This process is repeated until the condition condition is false.
The following is an example of using a While loop to output the numbers 1 to 5:
$ awk 'BEGIN {i = 1; while (i < 6) { print i; ++i } }'
The output result is:
1
2
3
4
5
Break
The break is used to end the loop:
In the following example, the loop is ended with break when the calculated sum is greater than 50:
$ 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 loop within the loop body, thus directly entering 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 an argument to indicate the exit status of the AWK process. If this argument is not provided, the default status