In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about the working principle of AWK. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
The AWK workflow can be divided into three parts:
The code snippet executed before reading the input file (identified by the BEGIN keyword).
The main loop executes the code snippet of the input file.
The code snippet (identified by the END keyword) after reading the input file.
Command structure:
Awk'BEGIN {commands} pattern {commands} END {commands}'
The workflow of AWK is described below:
1. Execute the content of the BEGIN block through the keyword BEGIN, that is, the content of the curly braces {} after the BEGIN.
2. Finish the execution of BEGIN block and start the execution of body block.
3. Read the record with\ nnewline division.
4. Divide the record into fields according to the specified domain delimiter and fill the domain. $0 represents all fields (that is, one line of content), $1 represents the first domain, and $n represents the nth domain.
5. Execute each BODY block in turn, and the content of the awk-commands will be executed only after the pattern partially matches the content of the line.
6. Read and execute each line in a loop until the end of the file to complete the execution of the body block.
7. Start the END block execution, and the END block can output the final result.
Start block (BEGIN)
The syntax format of the start block is as follows:
BEGIN {awk-commands}
The starting block is the part of the code that is executed when the program starts, and it is executed only once in the entire process.
In general, we can initialize some variables in the start block.
BEGIN is the keyword of AWK, so it must be uppercase.
Note: the start block section is optional, and your program may not have the start block part.
Body block (BODY)
The syntax format of the body part is as follows:
/ pattern/ {awk-commands}
The body command is executed once for each entered line.
By default, AWK executes the command for each line you enter. However, we can limit it to the specified mode.
Note: no keywords exist in the body block section.
End block (END)
The syntax format of the closing block is as follows:
END {awk-commands}
The closing block is the code that executes at the end of the program. END is also a keyword for AWK, and it must also be capitalized. Like the start block, the end block is optional.
Case study
First create a file called marks.txt. This includes the serial number, the student's name, the course name and the score.
1) Zhang San Chinese 80
2) Li Si Mathematics 90
3) Wang Wu English 87
Next, we will use the AWK script to display the contents of the output file and output the header information.
$awk'BEGIN {printf "serial number\ t name\ t course\ t score\ n"} {print} 'marks.txt
Execute the above command, and the output is as follows:
Serial number name course score
1) Zhang San Chinese 80
2) Li Si Mathematics 90
3) Wang Wu English 87
When the program starts execution, AWK outputs header information in the start block. In the body block, AWK outputs the read-in to the standard output stream for each line it reads until the entire file is read in.
This is how the editor shares the working principle of AWK. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.