Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use awk compound expressions

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the awk compound expression how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have something to gain after reading this awk compound expression article, let's take a look at it.

The compound expression of awk can consist of the combination operator "&" for "and" and "|" for "OR".

The general way to write a compound expression is as follows:

(first expression) & (second expression)

Here, the entire expression is true only if both the first expression and the second expression are true.

(first expression) | | (second expression)

Here, as long as the first expression is true or the second expression is true, the whole expression is true.

Note: remember to add parentheses.

Expressions can be made up of comparison operators.

Now let's deepen our understanding with an example:

In this example, there is a text file tecmint_deals.txt that contains a random list of Tecmint transactions with name, price, and category.

TecMint Deal ListNo Name Price Type1 Mac_OS_X_Cleanup_Suite $9.99 Software2 Basics_Notebook $14.99 Lifestyle3 Tactical_Pen $25.99 Lifestyle4 Scapple $19.00 Unknown5 Nano_Tool_Pack $11.99 Unknown6 Ditto_Bluetooth_Altering_Device $33.00 Tech7 Nano_Prowler_Mini_Drone $36.99 Tech

We just want to print out items that cost more than $20 and whose type is "Tech" and mark them with (*) at the end of the line.

We are going to carry out the following orders.

# awk'($3 ~ / ^ / $[2-9] [0-9] * /. [0-9] [0-9] $/) & ($4 million = "Tech") {printf "% s/t%s/n", $0, "*";} 'tecmint_deals.txt6 Ditto_Bluetooth_Altering_Device $33.00 Tech * 7 Nano_Prowler_Mini_Drone $36.99 Tech *

In this example, we used two expressions in the compound expression:

Expression 1: (2-9 impulse. 0-9; find lines whose transaction price is more than 20, that is, the value is true only if the price satisfies 2-9 shock. 0-9 $/. Expression 2: (; find out if there is a kind of transaction, that is, the value is true only if 4 equals "Tech". Keep in mind that this line is marked (*) only if the states at both ends of the & & operator, that is, both expressions are true. This is the end of the article on "how to use awk compound expressions". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use awk compound expressions". If you want to learn more, you are welcome to follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report