As a versatile text processing language, awk allows you to easily parse and transform text files. One of its most powerful features is the ability to execute conditional logic. In this comprehensive guide, we‘ll explore how to leverage awk‘s conditional statements to unlock its full potential.
If Statement
The basic if statement in awk follows this syntax:
if (condition) {
action1
}
If the condition evaluates to true, action1 will execute. Let‘s look at a simple example:
$ cat data.txt
1 10
2 20
3 30
$ awk ‘{if ($2 > 15) print $0}‘ data.txt
2 20
3 30
Here we print the entire line ($0) if the second field ($2) is greater than 15. The if statement allows us to selectively process lines that meet a criteria.
We can build on this by adding an else clause:
$ awk ‘{if ($2 > 15) print $0; else print "Nope"}‘ data.txt
Nope
2 20
3 30
Now lines that don‘t satisfy the condition will print "Nope".
Multi-way Conditional: if-else if-else
For more advanced logic, we can chain multiple if-else statements:
if (condition1) {
action1
} else if (condition2) {
action2
} else {
action3
}
If condition1 is true, action1 executes. Else if condition2 is true, action2 executes. Finally, the else clause handles all other cases with action3.
Here‘s an example:
$ cat data.txt
Tom 35 50000
John 40 60000
Sarah 28 45000
$ awk ‘{if ($2 < 30) print "Young"; else if ($2 >= 30 && $2 < 40) print "Middle-aged"; else print "Senior"}‘ data.txt
Young
Senior
Middle-aged
Based on the age in field 2, we classify people into age buckets. The power here is we can handle multiple conditional cases easily.
Ternary Operator
Awk also offers a ternary operator for quick if-else statements:
condition ? action1 : action2
If condition is true, action1 is executed. Otherwise, action2 is executed.
For example:
$ awk ‘{print ($3 > 50000 ? "High earner" : "Low earner")}‘ data.txt
Low earner
High earner
Low earner
This concise operator allows simple conditional logic to be written in one line.
Combining Conditions (&&, ||)
We can combine multiple conditionals using logical AND (&&) and OR (||):
if (condition1 && condition2) {
action1
}
if (condition1 || condition2) {
action2
}
Looking at our data, let‘s print only lines where the age is above 30 AND salary above 50000:
$ awk ‘{if ($2 > 30 && $3 > 50000) print $0}‘ data.txt
John 40 60000
Using && ensures both conditionals must be true. Similarly, || would print lines where EITHER condition is true.
Matching Regex Patterns
So far our conditions focused on numeric comparisons. But we can also match on regex patterns:
$ cat names.txt
Sachin Tendulkar
Ricky Ponting
Brian Lara
$ awk ‘{if ($0 ~ /^Sachin/) print "Match"}‘ names.txt
Match
The ~ operator checks if a regex pattern matches the current line. Here we print "Match" if a line starts with "Sachin".
We can leverage this to process log files based on matching meaningful patterns.
Iterating Arrays
In addition to on a per line basis, conditional logic in awk can involve iterating arrays:
arr[1] = 5
arr[2] = 10
for (i in arr) {
if (arr[i] > 5) {
print arr[i]
}
}
This would print the second element 10. Arrays allow more complex data structures for conditional testing.
Conclusion
Whether using basic if statements or complex regex matching, awk handles conditional logic with ease. Chaining multiple cases with if, else if, else provides the flexibility to handle real-world scenarios. By mastering conditional techniques in awk, one can truly unlock its power for advanced text processing tasks.
The concise syntax, array handling, and regex support enables awk to serve as an efficient scripting language. Understanding awk conditionals provides a solid foundation towards leveraging it for data science, system administration, log parsing, and many other use cases.


