In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "BEGIN and END schema case analysis of Awk". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Awk is a very good language in Unix environment, which is suitable for text processing and report generation. It also has many well-designed features that allow for special skill programming. For short messages, such as dealing with phone bill files.
As we unfold and explore more ways to build complex awk operations, we will prove how powerful these special features of awk are.
Before we begin, let's review the introduction to the awk series, remembering that when we started this series, I pointed out that the general syntax of the awk instruction is as follows:
# awk 'script' filenames
In the above syntax, the awk script has the following form:
/ pattern/ {actions}
You will usually find that the pattern (/ pattern) in the script is a regular expression. In addition, you can also use the special patterns BENGIN and END here. Therefore, we can also write an awk command in the following form:
Awk 'BEGIN {actions} / pattern/ {actions} / pattern/ {actions} … .end {actions}' filenames
If you use special modes in your awk script: BEGIN and END, here's what they mean:
BEGIN mode: means that awk will perform the actions specified in BEGIN immediately before reading any input lines. END mode: means that awk will perform the actions specified in END before it officially exits.
The execution flow of an awk command script with these special modes is as follows:
When BEGIN mode is used in the script, all actions in BEGIN are performed before any input lines are read. Then, read in an input line and parse it into different segments. Next, each specified non-special pattern is compared with the input line, and when the match is successful, the action corresponding to the pattern is performed. Repeat this step for all modes you specify. Next, repeat steps 2 and 3 for all input lines. When all input lines are read and processed, if you specify the END mode, the corresponding action will be performed.
To get the best results in awk operations when you use a special mode, you should remember the order of execution above.
For ease of understanding, let's demonstrate using the example in section 8, which is a list of domain names owned by Tecmint and saved in a file called domains.txt.
News.tecmint.comtecmint.comlinuxsay.comwindows.tecmint.comtecmint.comnews.tecmint.comtecmint.comlinuxsay.comtecmint.comnews.tecmint.comtecmint.comlinuxsay.comwindows.tecmint.comtecmint.com$ cat ~ / domains.txt
View the contents of the file
In this example, we want to count the number of occurrences of the domain name tecmint.com in the domains.txt file. So, we wrote a simple shell script to help us with the task, using the idea of variables, mathematical expressions, and assignment operators. The script reads as follows:
#! / bin/bashfor file in $@; doif [- f $file]; then### output file name echo "File is: $file" # output an incremented digital record containing the number of lines of tecmint.com awk'/ ^ tecmint.com/ {counter+=1; printf "% szone", counter;}'$fileelse### if the input is not a file, the output error message echo "$file is not a file, please specify a file." > & 2 & & exit 1fidone### terminates the script exit 0 with exit code 0 after successful execution
Now let's apply these two special modes to the awk command of the above script as follows: BEGIN and END:
We should put the script:
Awk'/ ^ tecmint.com/ {counter+=1; printf "% s Association", counter;}'$file
Change it to:
The number of tecmint.com occurrences in the awk 'BEGIN {print "file is:";} / ^ tecmint.com/ {counter+=1;} END {printf "% Sapo", counter;}' $file
After modifying the awk command, the complete shell script now looks like this:
#! / bin/bashfor file in $@; doif [- f $file]; then### output filename echo "File is: $file" # the total number of tecmint.com occurrences in the output file awk 'BEGIN {print "the number of times tecmint.com appears in the file is:";} / ^ tecmint.com/ {counter+=1;} END {printf "% sstructure", counter }'$fileelse### if the input is not a file, the output error message echo "$file is not a file, please specify a file." > & 2 & & exit 1fidone### terminates the scripts exit 0awk mode BEGIN and END with exit code 0 after successful execution
When we run the script above, it will first output the location of the domains.txt file, and then execute the awk command script, where the special mode BEGIN will help us output the message "the number of times tecmint.com appears in the file is:" before reading any lines from the file.
Next, our pattern / ^ tecmint.com/ is compared on each input line, and the corresponding action {counter+=1;} is executed on each line that matches successfully, which counts the number of tecmint.com occurrences in the file.
Finally, the END mode will output the total number of times the domain name tecmint.com appears in the file.
That's all for $. / script.sh ~ / domains.txt "BEGIN and END pattern instance Analysis of Awk". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.