While you evaluate all of the Awk examples we’ve got lined thus far, proper from the beginning of the Awk collection, you’ll discover that each one the instructions within the varied examples are executed sequentially, that’s one after the opposite.
However in sure conditions, we could need to run some textual content filtering operations primarily based on some situations, that’s the place the method of stream management statements is available in.
There are numerous stream management statements in Awk programming and these embrace:
if-else assertion
for assertion
whereas assertion
do-while assertion
break assertion
proceed assertion
subsequent assertion
nextfile assertion
exit assertion
Nonetheless, for the scope of this collection, we will expound on: if-else, for, whereas and do whereas statements. Keep in mind that we already walked by methods to use subsequent assertion in Half 6 of this Awk collection.
1. The if-else Assertion
The anticipated syntax of the if assertion is much like that of the shell if assertion:
if (condition1) {
actions1
}
else {
actions2
}
Within the above syntax, condition1 and condition2 are Awk expressions, and actions1 and actions2 are Awk instructions executed when the respective situations are happy.
When condition1 is happy, that means it’s true, then actions1 is executed and the if assertion exits, in any other case actions2 is executed.
The if assertion will also be expanded to a if-else_if-else assertion as beneath:
if (condition1){
actions1
}
else if (conditions2){
actions2
}
else{
actions3
}
On this type, if condition1 is true, then actions1 is executed and the if assertion exits, in any other case condition2 is evaluated and whether it is true, then actions2 is executed and the if assertion exits. Nonetheless, when condition2 is fake then, actions3 is executed and the if assertion exits.
Here’s a living proof of utilizing if statements, we’ve got an inventory of customers and their ages saved within the file customers.txt.
Sarah L 35 F
Aaron Kili 40 M
John Doo 20 M
Kili Seth 49 M
We need to print an announcement indicating a person’s title and whether or not the person’s age is much less or greater than 25 years previous.
We are able to write a brief shell script to hold out our job above, right here is the content material of the script:
#!/bin/bash
awk ‘ {
if ( $3 <= 25 ){
print “Person”,$1,$2,”is lower than 25 years previous.” ;
}
else {
print “Person”,$1,$2,”is greater than 25 years previous” ;
}
}’ ~/customers.txt
Then save the file and exit, make the script executable, and run it as follows:
chmod +x take a look at.sh
./take a look at.sh
Pattern Output:
Person Sarah L is greater than 25 years previous
Person Aaron Kili is greater than 25 years previous
Person John Doo is lower than 25 years previous.
Person Kili Seth is greater than 25 years previous
2. The for Assertion
In case you need to execute some Awk instructions in a loop, then the for assertion affords you an appropriate means to try this, with the syntax beneath:
for ( counter-initialization; test-condition; counter-increment ){
actions
}
Right here, the method is just outlined by way of a counter to manage the loop execution, first, you must initialize the counter, then run it towards a take a look at situation, whether it is true, execute the actions, and at last increment the counter. The loop terminates when the counter doesn’t fulfill the situation.
The next Awk command reveals how the for assertion works, the place we need to print the numbers 0-10:
awk ‘BEGIN{ for(counter=0;counter<=10;counter++){ print counter} }’
Pattern Output:
0
1
2
3
4
5
6
7
8
9
10
3. The whereas Assertion
The standard syntax of the whereas assertion is as follows:
whereas ( situation ) {
actions
}
The situation is an Awk expression and actions are strains of Awk instructions executed when the situation is true.
Beneath is a script for instance the usage of whereas assertion to print the numbers 0-10:
#!/bin/bash
awk ‘ BEGIN{ counter=0 ;
whereas(counter<=10){
print counter;
counter+=1 ;
}
}
Save the file and make the script executable, then run it:
chmod +x take a look at.sh
./take a look at.sh
Pattern Output:
0
1
2
3
4
5
6
7
8
9
10
4. The do whereas Assertion
The do-while assertion is a modification of the whereas assertion with the next syntax:
do {
actions
}
whereas (situation)
The slight distinction is that, below do whereas, the Awk instructions are executed earlier than the situation is evaluated. Utilizing the very instance below whereas assertion above, we are able to illustrate the usage of do whereas by altering the Awk command within the take a look at.sh script as follows:
#!/bin/bash
awk ‘ BEGIN{ counter=0 ;
do{
print counter;
counter+=1 ;
}
whereas (counter<=10)
}
‘
After modifying the script, save the file and exit. Then make the script executable and execute it as follows:
chmod +x take a look at.sh
./take a look at.sh
Pattern Output:
0
1
2
3
4
5
6
7
8
9
10
Conclusion
This isn’t a complete information relating to Awk stream management statements, as I had talked about earlier on, there are a number of different stream management statements in Awk.
Nonetheless, this a part of the Awk collection ought to provide you with a transparent basic thought of how the execution of Awk instructions will be managed primarily based on sure situations.
You may as well expound extra on the remainder of the stream management statements to achieve extra understanding of the subject material. Lastly, within the subsequent part of the Awk collection, we will transfer into writing Awk scripts.
For these looking for a complete useful resource, we’ve compiled all of the Awk collection articles right into a ebook, that features 13 chapters and spans 41 pages, protecting each fundamental and superior Awk utilization with sensible examples.
Product Title
Worth
Purchase
eBook: Introducing the Awk Getting Began Information for Newcomers
$8.99
[Buy Now]