Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use awk's special modes BEGIN and END

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use awk's special modes BEGIN and END. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

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 BEGIN 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.

When you use a special mode, if you want to get the results of the awk operation, you should keep in mind the above execution order.

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.com tecmint.com linuxsay.com windows.tecmint.com tecmint.com news.tecmint.com tecmint.com linuxsay.com tecmint.com news.tecmint.com tecmint.com linuxsay.com windows.tecmint.com tecmint.com

$cat ~ / domains.txt

Look at the contents of the file. In this example, we want to count the number of times the domain name tecmint.com appears 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/bash for file in $@; do if [- f $file]; then # output file name echo "File is: $file" # output an incremental digital record containing the number of lines of tecmint.com awk'/ ^ tecmint.com/ {counter+=1; printf "% s\ n", counter;}'$file else # if the input is not a file, the output error message echo "$file is not a file, please specify a file." > & 2 & & exit 1 fi done # after successful execution, terminate the script exit 0 with exit code 0

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\ n", counter;}'$file

Change it to:

The number of tecmint.com occurrences in the awk 'BEGIN {print "file is:";} / ^ tecmint.com/ {counter+=1;} END {printf "% s\ n", counter;}' $file

After modifying the awk command, the complete shell script now looks like this:

#! / bin/bash

For file in $@; do

If [- f $file]; then

# output file name

Echo "File is: $file"

# Total number of tecmint.com occurrences in the output file

The number of times tecmint.com appears in the awk 'BEGIN {print "file is:";}

/ ^ tecmint.com/ {counter+=1;}

END {printf "% s\ n", counter;}

'$file

Else

# if the input is not a file, output error message

Echo "$file is not a file, please specify a file." > & 2 & & exit 1

Fi

Done

# terminate the script with exit code 0 after successful execution

Exit 0

Awk modes BEGIN and END when we run the script above, it will first output the location of the domains.txt file, and then execute the awk command script, the special mode BEGIN in this command script 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.

$. / script.sh ~ / domains.txt this is the end of the article on "how to use BEGIN and END in awk". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report