How to Use Comparison Operators with Awk in Linux – Part 4

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

9 Responses

  1. Dan says:

    Apparently the script is not smart enough to validate if $3 is a number of character.

    $ awk '$3 <= 30 { print $0," 30 { print $0, "<-- quantity greater than 30" ;}' food_list.txt
    
    No      Item_Name               Quantity        Price <-- quantity greater than 30
    1       Mangoes                    45           $3.45 <-- quantity greater than 30
    2       Apples                     25           $2.45 <-- quantity is less than or equal to 30
    3       Pineapples                 5            $4.45 <-- quantity is less than or equal to 30
    4       Tomatoes                   25           $3.45 <-- quantity is less than or equal to 30
    5       Onions                     15           $1.45 <-- quantity is less than or equal to 30
    6       Bananas                    30           $3.45 <-- quantity is less than or equal to 30
    
    • Aaron Kili says:

      @Dan

      Sure, you can modify it for that purpose and share it with us.

      • Dan says:

        My try script, while the output is good however I do feel it’s a bit redundant particularly checking ($3 ~ /^[0-9]/) twice. Please simply :)

        $ awk ‘($3 ~ “^[a-zA-Z]”) { print $0} (($3 ~ /^[0-9]/) && ($3 <= 30)) { print $0," 30)) { print $0, “<– quantity is greater than 30" ;}' food_list.txt

        No      Item_Name               Quantity        Price
        1       Mangoes                    45           $3.45 <-- quantity is greater than 30
        2       Apples                     25           $2.45 <-- quantity is less than or equal to 30
        3       Pineapples                 5            $4.45 <-- quantity is less than or equal to 30
        4       Tomatoes                   25           $3.45 <-- quantity is less than or equal to 30
        5       Onions                     15           $1.45 <-- quantity is less than or equal to 30
        6       Bananas                    30           $3.45 <-- quantity is less than or equal to 30
        
  2. upkar says:

    What is the difference between using these expression directly and using this expressions with if statement. How it makes difference.

  3. jumen says:

    awk ‘NR==1 {print }; NR>1 && $3 > 30 { printf “%s\t%s\n”, $0,”**” ; };NR>1 && $3 <= 30 { print $0 ;}' food_list.txt
    will be better

  4. jumen says:

    if I want filter the quantity greater than 30; there will be something wrong
    awk ‘$3 > 30 { printf “%s\t%s\n”, $0,”**” ; } $3 1 && $3 > 30 { printf “%s\t%s\n”, $0,”**” ; };NR>1 && $3 <= 30 { print $0 ;}' food_list.txt

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.