How to Use ‘next’ Command with Awk in Linux – Part 6

In this sixth part of Awk series, we shall look at using next command, which tells Awk to skip all remaining patterns and expressions that you have provided, but instead read the next input line.

The next command helps you to prevent executing what I would refer to as time-wasting steps in a command execution.

To understand how it works, let us consider a file called food_list.txt that looks like this:

Food List Items
No      Item_Name               Price           Quantity
1       Mangoes                 $3.45              5
2       Apples                  $2.45              25
3       Pineapples              $4.45              55
4       Tomatoes                $3.45              25
5       Onions                  $1.45              15
6       Bananas                 $3.45              30

Consider running the following command that will flag food items whose quantity is less than or equal to 20 with a (*) sign at the end of each line:

# awk '$4 <= 20 { printf "%s\t%s\n", $0,"*" ; } $4 > 20 { print $0 ;} ' food_list.txt 

No	Item_Name		Price		Quantity
1	Mangoes			$3.45		   5	*
2	Apples			$2.45              25
3	Pineapples		$4.45              55
4	Tomatoes		$3.45              25 
5	Onions			$1.45              15	*
6	Bananas	                $3.45              30

The command above actually works as follows:

  1. First, it checks whether the quantity, fourth field of each input line is less than or equal to 20, if a value meets that condition, it is printed and flagged with the (*) sign at the end using expression one: $4 <= 20
  2. Secondly, it checks if the fourth field of each input line is greater than 20, and if a line meets the condition it gets printed using expression two: $4 > 20

But there is one problem here, when the first expression is executed, a line that we want to flag is printed using: { printf "%s\t%s\n", $0,"**" ; } and then in the same step, the second expression is also checked which becomes a time wasting factor.

So there is no need to execute the second expression, $4 > 20 again after printing already flagged lines that have been printed using the first expression.

To deal with this problem, you have to use the next command as follows:

# awk '$4 <= 20 { printf "%s\t%s\n", $0,"*" ; next; } $4 > 20 { print $0 ;} ' food_list.txt

No	Item_Name		Price		Quantity
1	Mangoes			$3.45		   5	*
2	Apples			$2.45              25
3	Pineapples		$4.45              55
4	Tomatoes		$3.45              25 
5	Onions			$1.45              15	*
6	Bananas	                $3.45              30

After a single input line is printed using $4 <= 20 { printf "%s\t%s\n", $0,"*" ; next ; }, the next command included will help skip the second expression $4 > 20 { print $0 ;}, so execution goes to the next input line without having to waste time on checking whether the quantity is greater than 20.

The next command is very important is writing efficient commands and where necessary, you can always use to speed up the execution of a script. Prepare for the next part of the series where we shall look at using standard input (STDIN) as input for Awk.

Hope you find this how to guide helpful and you can as always put your thoughts in writing by leaving a comment in the comment section below.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

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...

2 Responses

  1. Dan says:

    1) What is the different if I simply remove $4 > 20 { print $0, “-” ;} since it’s not processing at all?

    awk ‘$4 <= 20 { printf "%s\t%s\n", $0,"*"}' food_list.txt

    1       Mangoes                 $3.45              5    *
    5       Onions                  $1.45              15   *
    

    2) What does it do? printf "%s\t%s\n"

    • Edgar says:

      Each line is read twice to do the comparison if it is bigger than 20 or less than 20. What I believe this is doing is that once it found that the first condition was met it skips the second validation and goes to the next line.

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.