In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 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 write scripts in awk language. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Much like writing a shell script, the awk script begins with the following line:
#! / path/to/awk/utility-f
For example, on my system, the awk tool is installed in the / user/bin/awk directory, so my awk script starts with the following:
#! / usr/bin/awk-f
The above line is explained as follows:
#!, called Shebang, indicates which interpreter to use to execute commands in the script
/ usr/bin/awk, the interpreter
-f, the interpreter option, used to specify the program file to be read
Having said that, let's delve deeper into some executable awk scripts, starting with the following simple example. Create a new file using your favorite editor, like this:
$vi script.awk
Then paste the following code into the file:
#! / usr/bin/awk-fBEGIN {printf "% s\ n", "Writing my first awk executable script!"}
Save the file and exit, and then execute the following command to make the script executable:
$chmod + x script.awk
Then, execute it:
$. / script.awk
Sample output:
Writing my first awk executable script!
A strict programmer must ask, "where are the comments?" Yes, you can include comments in the awk script. It is a good programming habit to write comments in your code.
It helps other programmers to read your code and understand the functions of each part of the program file or script.
So, you can add comments to the script as follows:
#! / usr/bin/awk-f # this is an example of how to write comments in awk # use the special mode BEGIN to output a sentence BEGIN {printf "% s\ n", "Writing my first awk executable script!"}
Next, let's look at an example of reading a file. We want to find a user named aaronkilik from the account file / etc/passwd, and then print the user name, the user's ID, and the user's GID as follows:
Here is the contents of our script file, named second.awk.
#! / usr/bin/awk-f # uses the BEGIN specified character to set the FS built-in variable BEGIN {FS= ":"} # search for user name aaronkilik and output account details / aaronkilik/ {print "Username:", $1, "User ID:", $3, "User GID:", $4}
Save the file and exit, make the script executable, and then execute it as follows:
$chmod + x second.awk$. / second.awk / etc/passwd
Sample output:
Username: aaronkilik User ID: 1000 User GID: 1000
In the following * example, we will use the do while statement to print the numbers 0-10:
Here is the contents of our script file, named do.awk.
#! / usr/bin/awk-f # printing from 0-10 using a do while statement # do while statement BEGIN {# initialize a counter xroom0 do {print x; xbirthday 1;} while (x
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: 202
*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.