How to Use the 'Next' command of awk

Source: Internet
Author: User

How to Use the 'Next' command of awk
GuideIn the awk series, let's take a look at the next command, which tells awk to skip all the remaining modes and expressions you provide and directly process the next input line. The next command helps you stop unnecessary steps during command execution.


To understand how it works, let's analyze food_list.txt and it looks like this:

Food List ItemsNo      Item_Name               Price           Quantity1       Mangoes                 $3.45              52       Apples                  $2.45              253       Pineapples              $4.45              554       Tomatoes                $3.45              255       Onions                  $1.45              156       Bananas                 $3.45              30>

Run the following command, which marks an asterisk after the number of rows with a food value less than or equal to 20:

# awk '$4 <= 20 { printf "%s/t%s/n", $0,"*" ; } $4 > 20 { print $0 ;} ' food_list.txt No    Item_Name     Price      Quantity1     Mangoes       $3.45       5    *2     Apples        $2.45      253     Pineapples    $4.45      554     Tomatoes      $3.45      25 5     Onions        $1.45      15   *6     Bananas       $3.45      30
The preceding command is actually run as follows:
1. it uses a $4 <= 20 expression to check whether the fourth column (Quantity) of each input row is smaller than or equal to 20. If the condition is met, it will end with an asterisk (*).
2. It uses a $4> 20 expression to check whether the fourth column of each input row is greater than 20. If the conditions are met, it is displayed.

But there is a problem here, when the first expression uses {printf "% s/t % s/n", $0 ,"**";} when the command is labeled, the second expression in the same step is also judged, which wastes time. Therefore, when we use the first expression to print the flag line, we do not need to use the second expression $4> 20 to print again.

To solve this problem, we need to use the next command:

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

When the input line is printed with the $4 <= 20 {printf "% s/t % s/n", $0, "*"; next;} command, the next command skips the second $4> 20 {print $0;} expression and continues to judge the next input line, instead of wasting time to determine whether the current input line is greater than 20.

The next command is very important when writing an efficient command script, which can speed up the script.

From: https://linux.cn/article-7609-1.html

Address: http://www.linuxprobe.com/awk-next-use.html


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.