Awk programming model and awk Programming
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.
References: http://www.linuxawk.com/communication/459.html