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 the comparison operator of awk

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "awk comparison operator how to use", in daily operation, I believe many people in awk comparison operator how to use the problem there are doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "awk comparison operator how to use" doubts helpful! Next, please follow the small series to learn together!

Awk is a very good language for Unix environment, suitable for text processing and report generation, when text processing to use comparison operators to filter text and strings is a very good choice.

What is the comparison operator in awk?

Comparison operators in awk are used to compare strings and or numbers, and include the following types:

Symbol effect> greater than

Less than>= Greater than or equal to

Less than == not equal to some_value ~ / pattern/truesome_value !~ if some_value matches pattern / pattern/returns true if some_value does not match pattern

Now let's familiarize ourselves with the various comparison operators in awk by example.

For example 1, we have a file named food_list.txt that contains a list of purchases of different foods. I want to add (**) to the end of the row for items with 30 or fewer foods

File – food_list.txtNo Item_Name Quantity Price1 Mangoes 45 $3.452 Apples 25 $2.453 Pineapples 5 $4.454 Tomatoes 25 $3.455 Onions 15 $1.456 Bananas 30 $3.45

The common syntax for using comparison operators in Awk is as follows:

#Expression {action; }

To accomplish this, execute the following command:

# awk '$3 30 { print $0 ;}' food_list.txtNo Item_Name` Quantity Price1 Mangoes 45 $3.452 Apples 25 $2.45 **3 Pineapples 5 $4.45 **4 Tomatoes 25 $3.45 **5 Onions 15 $1.45 **6 Bananas 30 $3.45 **

In this example, two important things happen:

The first expression {Action;} in the combination,$3; } prints out 30 or fewer lines followed by (). The number of items is obtained through the $3 field variable. In the second "Expression {Action;}" combination,$3 > 30 { print $0 ;} outputs lines with a number of less than or equal to 30 as is.

Another example:

# awk '$3 20 { print $0 ;} ' food_list.txtNo Item_Name Quantity Price1 Mangoes 45 $3.452 Apples 25 $2.453 Pineapples 5 $4.45 TRUE4 Tomatoes 25 $3.455 Onions 15 $1.45 TRUE6 Bananas 30 $3.45

In this example, we want to mark rows with a number less than or equal to 20 by incrementing (TRUE) at the end of the row.

At this point, the study of "how to use awk comparison operators" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

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