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.
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.
Thanks, Ren!!
Correct answer. You hit the mail.
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.
Point taken, will do as you have mentioned.
@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
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.