Home > System Tutorial > LINUX > How to use comparison operators in awk

How to use comparison operators in awk

WBOY
Release: 2023-12-31 21:35:33
forward
1365 people have browsed it

For users who use the awk command, when processing numbers or strings in a line of text, it is very convenient to use comparison operators to filter text and strings. In the following section we introduce the comparison operators of "awk".

What are the comparison operators in awk?

Comparison operators in awk are used to compare strings and values, including the following types:

Symbol Function

>                                                Greater than

<                                                        Less than

>=          Greater than or equal to

<=                                                                                                                                                                 shall be less than or equal to

==       Equal to

!= Not equal to

some_value ~ / pattern/ Returns true if some_value matches pattern pattern

some_value !~ / pattern/ Returns true if some_value does not match the pattern pattern Now let's familiarize ourselves with the various comparison operators in awk through examples.

Example 1, we have a file named food_list.txt, which contains purchase lists of different foods. I want to add (**)

after the row of items whose food quantity is less than or equal to 30

File – food_list.txt

No Item_Name Quantity Price

1 Mangoes 45 $3.45

2 Apples 25 $2.45

3 Pineapples 5 $4.45

4 Tomatoes 25 $3.45

5 Onions 15 $1.45

6 Bananas 30 $3.45

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

# Expression { action; }

In order to achieve the purpose just now, execute the following command:

# awk '$3 <= 30 { printf "%s/t%s/n", $0,"**" ; } $3 > 30 { print $0 ;}' food_list.txt

No Item_Name` Quantity Price

1 Mangoes 45 $3.45

2 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 the example just now, the following two important things happened:

In the first "expression {action;}" combination, $3 <= 30 { printf "%s/t%s/n", $0,"**"; } print Extract rows whose number is less than or equal to 30, and add (**) at the end. The quantity of the item is obtained through the $3 field variable. In the second "expression {action;}" combination, $3 > 30 { print $0 ;} will output the rows whose number is less than or equal to 30 as is.

Another example:

# awk '$3 <= 20 { printf "%s/t%s/n", $0,"TRUE" ; } $3 > 20 { print $0 ;} ' food_list.txt

No Item_Name Quantity Price

1 Mangoes 45 $3.45

2 Apples 25 $2.45

3 Pineapples 5 $4.45 TRUE

4 Tomatoes 25 $3.45

5 Onions 15 $1.45 TRUE

6 Bananas 30 $3.45

In this example, we want to mark rows whose number is less than or equal to 20 by adding (TRUE) to the end of the row.

Summarize

This is an introductory guide to the comparison operators in awk, so you will need to try other options and discover more ways to use them.

The above is the detailed content of How to use comparison operators in awk. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template