Awk Programming Model

Source: Internet
Author: User

Awk Programming Model

It is important to understand the basic model that awk provides to programmers. Learning awk is easier than learning other programming languages because awk provides programmers with well-defined and useful models.

The awk program is composed of a so-called main input loop. A loop is a routine that repeats until some conditions exist to terminate it. You don't have to write this loop. It is a ready-made one. It exists as a framework, and the code you write in this framework can be executed. The code you wrote for processing operations assumes that there is an input line available. In other programming languages, you must create a main input loop and use it as an integral part of the program. It must open an input file and read a row at a time. In other programming languages, you must create a main input loop and use it as an integral part of the program. It must open an input file and read a row at a time. It demonstrates that the basic simplified awk operations can make programming easier.

The number of times the main input loop is executed is the same as the number of input rows. As shown in the "Hello, World." example, this loop is executed only when there is an input. When no other input lines are read, the loop ends.

$ Awk 'in in {print "Hello, world "}'
Hello, world

Awk allows you to write two special routines that are executed before and after any input is read. They are related to the BEGIN and END rules. In other words, you can do some processing before and after the main input loop. The BEGIN and END processes are optional.

You can regard the awk script as composed of three main parts: the processing that will be done before the input is processed, the processing that will be done during the input process, and the processing after the input is completed. For these three components, the main input loop or "processing in progress" is the main processing part. In the main input loop, commands are written into a series of pattern/action processes. The mode is used to test the rules of input rows to determine whether the action will be applied to these input rows. The operation we will see may be very complicated, and it consists of statements, functions, and expressions.

The main thing to remember is that each mode/operation process is located in the main input loop and is responsible for reading input rows. The written process is applied to each input row and one row at a time.

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.