How to use the comparison operator for awk

Source: Internet
Author: User

for users using the awk command, it is convenient to use comparison operators to filter text and strings when working with numbers or strings in a line of text. The following section describes the comparison operators for "awk".

The comparison operators in awk are used to compare strings and or numeric values, including the following types:

symbol action
> greater than
< less than
>= greater than or equal to
<= less than equals
== equals
!= Not equal to
some_value ~/pattern/
some_value!~/pattern/ If Some_value does not match pattern patterns, it returns True

Now we are familiar with the various comparison operators in awk using examples.

Example one, we have a file named Food_list.txt, which contains a list of different food purchases. I would like to add (* *) to the following line of items where the food quantity is less than or equal to 30.

File–food_list.txtno      item_name               Quantity        Price1       mangoes           25 $3.452                     Apples           $2.453       Pineapples                 5            $4.454       tomatoes           $3.455       onions       -$1.456 Bananas           $3.45

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

# Expression {action;}

To achieve the purpose that you have just done, execute the following command:

# awk ' $ <= ' (printf "%s/t%s/n", $ A, "* *";} $ > {print $;} ' Food_list.txtno  item_name '      quantity< C2/>price1   mangoes       $3.452   Apples $2.45 **3 pineapples         5        $4.45   **4   Tomatoes       $3.45   **5   onions       $1.45       **6 Bananas   **

In just the example, there are two important things:

    • the first expression {action;} " In combination, the $ <= {printf "%s/t%s/n", "$", "* *", the number of lines that are less than or equal to 30 are printed, and added later (* *). The number of items is obtained through the domain variable of $ $.
    • the second expression {action;} " In combination, $ > {print $;} The output quantity is less than or equal to 30 lines.

One more example:

# awk ' $ <= ' (printf "%s/t%s/n", $ "TRUE";} $ >  {print $;} ' Food_list.txt No    item_name     Quantity    Price1     mangoes          '       $3.452     Apples       $ 2.453     Pineapples        5       $4.45 TRUE4     tomatoes         (       $3.455     onions       ) $1.45 TRUE6     Bananas       $3.45

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

Summary

This is a guide to the introduction of comparison operators in awk, so you need to try other options to find more ways to use them.

If you encounter or think of any problems, please leave a comment in the comments section below. Remember to read the next section of the Awk series, where I'll look at combining expressions.

Free to provide the latest Linux technology tutorials Books, for open-source technology enthusiasts to do more and better: http://www.linuxprobe.com/

How to use the comparison operator for awk

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.