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.

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Aaron Kili
Aaron Kili
Contributor
About the author

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.

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.