All along, we have been looking at simple expressions when checking whether a condition has been met or not. What if you want to use more than one expression to check for a particular condition?
In this article, we shall take a look at how you can combine multiple expressions referred to as compound expressions to check for a condition when filtering text or strings.
Please refer to our previous tutorials in the Awk series:
- How to Filter Text or String Using Awk and Regular Expressions – Part 1
- How to Use Awk to Print Fields and Columns in File – Part 2
- How to Use Awk to Filter Text Using Pattern-Specific Actions – Part 3
- How to Use Comparison Operators with Awk in Linux – Part 4
- How to Use Compound Expressions with Awk in Linux – Part 5
- How to Use ‘next’ Command with Awk in Linux – Part 6
- How to Read Awk Input from STDIN in Linux – Part 7
- How to Use Awk Variables, Numeric, and Assignment Operators – Part 8
- How to Use Awk Special Patterns ‘BEGIN and END’ – Part 9
- How to Use Awk Built-in Variables in Linux – Part 10
- How to Allow Awk to Use Shell Variables in Linux – Part 11
- How to Use Flow Control Statements in Awk – Part 12
- How to Write Scripts Using Awk Programming Language – Part 13
Compound Expressions in Awk
In Awk, compound expressions are built using the && referred to as (and) and the || referred to as (or) compound operators.
The general syntax for compound expressions is:
expression1 && expression2
The above syntax represents a logical AND operation, where both expression1 and expression2 must be true for the overall expression to be true.
Similarly, you can use the || operator for a logical OR operation.
expression1 || expression2
Here, one of the expressions is either expression1 or expression2 must be true for the whole expression to be true.
The expressions can be built using the comparison operators that we looked at in Part 4 of the awk series.
In this article, we’ll explore how to use compound expressions with Awk in Linux, using practical examples.
Example File
In this example, we have a text file named tecmint_deals.txt, which contains a list of some amazing random Tecmint deals, it includes the name of the deal, the price, and the type.
No Name Price Type 1 Mac_OS_X_Cleanup_Suite $9.99 Software 2 Basics_Notebook $14.99 Lifestyle 3 Tactical_Pen $25.99 Lifestyle 4 Scapple $19.00 Unknown 5 Nano_Tool_Pack $11.99 Unknown 6 Ditto_Bluetooth_Altering_Device $33.00 Tech 7 Nano_Prowler_Mini_Drone $36.99 Tech
Example 1: Filtering by Price Range
Suppose we want to filter products with a price between $10 and $20, we can use a compound expression in Awk as shown.
awk '$3 >= 10 && $3 <= 20 { print $0 }' tecmint_deals.txt
Sample Output:
1 Mac_OS_X_Cleanup_Suite $9.99 Software 4 Scapple $19.00 Unknown 5 Nano_Tool_Pack $11.99 Unknown
Example 2: Combining Conditions
Let’s say we want to filter products that are either of type “Software” or “Tech“. We can use the logical OR operator (||) in our compound expression.
awk '$4 == "Software" || $4 == "Tech" { print $0 }' tecmint_deals.txt
Sample Output:
1 Mac_OS_X_Cleanup_Suite $9.99 Software 6 Ditto_Bluetooth_Altering_Device $33.00 Tech 7 Nano_Prowler_Mini_Drone $36.99 Tech
Example 3: Combination of Conditions
Now, let’s combine both price and type conditions. Suppose we want to filter products that are priced below $20 and are of type “Unknown“. We can do this by combining two conditions with the logical AND operator (&&):
awk '$3 < 20 && $4 == "Unknown" { print $0 }' tecmint_deals.txt
Sample Output:
1 Mac_OS_X_Cleanup_Suite $9.99 Software 5 Nano_Tool_Pack $11.99 Unknown
Example 4: Negating Conditions
Lastly, let’s say we want to exclude products of type “Lifestyle“. We can achieve this by negating the condition using the logical NOT operator (!):
awk '$4 != "Lifestyle" { print $0 }' tecmint_deals.txt
Sample Output:
1 Mac_OS_X_Cleanup_Suite $9.99 Software 4 Scapple $19.00 Unknown 5 Nano_Tool_Pack $11.99 Unknown 6 Ditto_Bluetooth_Altering_Device $33.00 Tech 7 Nano_Prowler_Mini_Drone $36.99 Tech
Summary
Some conditions always require building compound expressions for you to match exactly what you want. When you understand the use of comparison and compound expression operators then, filtering text or strings based on some difficult conditions will become easy.
For those seeking a comprehensive resource, we’ve compiled all the Awk series articles into a book, that includes 13 chapters and spans 41 pages, covering both basic and advanced Awk usage with practical examples.
| Product Name | Price | Buy |
|---|---|---|
| eBook: Introducing the Awk Getting Started Guide for Beginners | $8.99 | [Buy Now] |
Hope you find this guide useful and for any questions or additions, always remember to leave a comment, and your concern will be solved accordingly.
If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.

Got Something to Say? Join the Discussion... Cancel reply