1. if-else statementAs you think. The if statement syntax is similar to the if statement in shell:
If (condition 1) {Action 1} else {Action 2}In the preceding syntax, condition 1 and condition 2 are awk expressions, and Action 1 and Action 2 are the awk commands executed when their respective conditions are met.
If condition 1 is true, Action 1 is executed and exited the if statement. Otherwise, action 2 is executed.
The if statement can also be extended to the following if-else_if-else statement:
If (condition 1) {Action 1} elseif (condition 2) {Action 2} else {Action 3}In the preceding format, if condition 1 is true, Action 1 is executed and exited the if statement. Otherwise, condition 2 is evaluated and if the value is true, then Action 2 is executed and exited the if statement. However, when condition 2 is false, Action 3 is executed and exits the if statement.
This is an example of using the if statement. We have a list of users and their ages stored in the file users.txt.
We need to print a list to show whether the user name and the user's age are less than or more than 25 years old.
aaronkilik@tecMint~$catusers.txtSarahL35FAaronKili40MJohnDoo20MKiliSeth49M
We can write a short shell script to execute our work in the above text, which is the content of the script:
#!/bin/bashawk'{ if($3<=25){ print"User",$1,$2,"islessthan25yearsold."; }else{ print"User",$1,$2,"ismorethan25yearsold"; }}'~/users.txtSave the file and exit. Run the script as follows:
$chmod+xtest.sh$./test.sh
Output example
UserSarahLismorethan25yearsoldUserAaronKiliismorethan25yearsoldUserJohnDooislessthan25yearsold.UserKiliSethismorethan25yearsold
2. for statementIf you want to execute some awk commands in a loop, the for statement provides you with an appropriate method for doing this, in the following format:
For (counter initialization; test conditions; counter addition) {action}Here, this method is defined by using a counter to control loop execution. First you need to initialize this counter and then run it for test conditions. If it is true, execute these actions and add the counter. When the counter does not meet the conditions, the cycle ends.
When we want to print numbers 0 to 10, the following awk command shows how the for statement works:
$awk'BEGIN{for(counter=0;counter<=10;counter++){printcounter}}'Output example
0 12345678910
3. while statementThe traditional syntax of the while statement is as follows:
While (condition) {action}This condition is an awk expression, and the action is the awk Command executed when the condition is true.
The following is a script that uses the while statement to print numbers 0 to 10:
#!/bin/bash awk'BEGIN{counter=0; while(counter<=10){ printcounter; counter+=1; }}'Save the file and make the script executable, and then run it:
$chmod+xtest.sh$./test.sh
Output example
0 12345678910
4. do while statementIt is a variant of the while statement in the above text and has the following syntax:
Do {action} while (condition)The slight difference is that in the do while statement, the awk command is executed before the evaluate condition. In the example of the while statement above, we can modify the awk command in the test. sh script as follows to describe the usage of the do while statement:
#!/bin/bashawk'BEGIN{counter=0; do{ printcounter; counter+=1;}while(counter<=10) }'After modifying the script, save the file and exit. Run the script as follows:
$chmod+xtest.sh$./test.sh
Output example
0 12345678910
5. SummaryThis is not a comprehensive guide to awk process control statements. As I mentioned earlier, there are several other process control statements in awk.
Even so, this part of the awk series allows you to understand the basic concept of how an awk command is executed based on certain conditions.
You can also learn more about the other process control statements to obtain