How to Use Compound Expressions with Awk in Linux – Part 5

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.

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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

5 Comments

Leave a Reply
  1. Do not use parenthesis when explaining you are “flagging the text with…” (**), when you are actually flagging it with just **. The special code formatting (the pink text box) already depicts that meaning. Putting extra parenthesis just makes it more cumbersome.
    The same is for “(and)” and “(or)” in the third paragraph.

    Reply
  2. @Ren

    The line is:
    7 Nano_Prowler_Mini_Drone $36.99 Tech

    Not:

    7 Nano_Prowler_Mini_Drone $3.99 Tech

    So it is supposed to be printed with a “*” since it is above $20

    Reply
  3. I think
    $ awk '($3 ~ /^\$[2-9][0-9]*\.[0-9][0-9]$/) && ($4=="Tech") { printf "%s\t%s\n",$0,"*"; } ' tecmint_deals.txt
    should change to
    $ awk '($3 ~ /^\$[2-9][0-9]+\.[0-9][0-9]$/) && ($4=="Tech") { printf "%s\t%s\n",$0,"*"; } ' tecmint_deals.txt
    otherwise, the following line will also printed with “*”:
    7 Nano_Prowler_Mini_Drone $3.99 Tech

    P.S. Sorry for my English.

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.