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.

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.

TecMint Deal List
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.

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.

Similar Posts