Awk control statement

Source: Internet
Author: User

Awk control statement

The break and continue statements can use the break statement to exit the loop when a specific condition is true. When a specific condition is true, the continue statement skips the continue statement, switches the statement back to the top of the loop, and starts the next loop.

Example

$ awk '{for(x = 3; x <= NF; x++){if($x < 0){ print "Bottomed out!"; break}/*break out of for loop*/}}' filename

Note: If the value of field $ x is less than 0, the break statement will control the statement that jumps to the right curly braces of the loop body, that is, the loop jumps out.

$ awk '{for(x = 3; x <= NF; x++){if($x < 0){ print "Bottomed out!"; continue}/*starts next iteration of the for loop*/}}' filename

Note: If the value of the field $ x is equal to 0, the continue statement converts the control back to the top of the loop and starts execution, starting with the 3rd expressions x ++ of the for loop.

Next statement
The next statement extracts the next line of input from the input file, and then re-runs from the top of the awk script.

Example

Awk '{
If ($1 ~ /Peter/) {next}
Else {print}
} 'Filename'

Note: If the first field of a row contains Peter, awk skips the row, reads the next line from the input file, and then executes the script from the beginning.

Exit statement
The exit statement is used to terminate the awk program. It can only interrupt the processing of records and cannot skip the END statement. If the exit statement parameter is a value (exit 1) between 0 and, the value is printed on the command line to indicate whether the program is successfully executed, and indicates the type of failure.

Example

$ Awk '{exit 1}' filename
$ Echo $?
1

Note: If the exit status is 0, the operation is successful. If the exit status is not 0, the operation fails (this is a unified convention of Linux ). The exit status is determined by the programmer whether the service is provided in the program. In this example, the exit status returned by the command is 1.

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.