In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use awk compound expressions, with certain reference value, interested friends can refer to, I hope you have a lot of gains after reading this article, let Xiaobian take you to understand.
So far, we've used simple expressions to check whether conditions match. What if you want to check a particular condition with more than one expression?
In this article, we'll see how to combine multiple expressions, compound expressions, to check conditions when filtering text and strings.
Compound expressions of awk can be represented by the combined operators && for AND and OR for|| Composition.
The general way to write a compound expression is as follows:
( *** expressions) && (second expression)
Here the whole expression is true only if both "*** expressions" and "second expression" are true.
( *** expressions)||(second expression)
Here the entire expression is true as long as either "*** expressions" are true or "second expression" is true.
Note: Remember to include brackets.
Expressions can consist of comparison operators, as described in Section 4 of the awk series.
Now let's deepen our understanding through an example:
In this example, there is a text file tecmint_deals.txt containing a random list of Tecmint transactions with names, prices, and categories.
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
We only want to print items that cost more than $20 and are in the category "Tech," marked with an (*) at the end of the line.
We're going to execute the following orders.
# awk '($3 ~ /^\$[2-9][0-9]*\. [0-9][0-9]$/) && ($4=="Tech") { printf "%s\t%s\n",$0,"*"; } ' tecmint_deals.txt 6 Ditto_Bluetooth_Altering_Device $33.00 Tech * 7 Nano_Prowler_Mini_Drone $36.99 Tech *
In this example, we use two expressions in the compound expression:
Expression 1:($3 ~ /^\$[2-9][0-9]*\. [0-9][0-9]$/) ; Find rows with transaction prices above $20, i.e. only if $3 is the price that satisfies/^\$[2-9][0-9]*\. [0-9][0-9]$/Value is true.
Expression 2:($4 == "Tech") ; finds if there is a transaction of type "Tech," i.e. the value is true only if $4 equals "Tech." Remember that this line is marked with an (*) only if both ends of the && operator are true, that is, both expressions are true.
Thank you for reading this article carefully. I hope that the article "How to use awk compound expressions" shared by Xiaobian will be helpful to everyone. At the same time, I hope that everyone will support you a lot and pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.