当前位置:网站首页>Awk from entry to earth (12) awk can also write scripts to replace the shell
Awk from entry to earth (12) awk can also write scripts to replace the shell
2022-07-04 08:46:00 【Wonderful binary】
summary
And write shell The script is pretty much , First, the first line declares the interpreter path , For example, on my system ,awk The tool is installed in /user/bin/awk Catalog , So my awk The script starts with the following :
#! /usr/bin/awk -f
The explanation of the above line is as follows :
- #! , It's called Shi Pang , Indicate which interpreter is used to execute the commands in the script ;
- /usr/bin/awk , Interpreter ;
- -f , Interpreter options , Used to specify the program file to read ;
Say so , Now start with the following simple example , Let's delve into some executable awk Script . Create a new file using your favorite editor , Like this :
$ vi script.awk
Then paste the following code into the file :
#!/usr/bin/awk -f
BEGIN { printf "%s\n","Writing my first awk executable script!" }
Save the file and exit , Then execute the following command , Make the script executable :
$ chmod +x script.awk
then , Execute it :
$ ./script.awk
sample output :
Writing my first awk executable script!
Add notes
A strict programmer will ask :“ A comment? ?”. Yes , You can awk The script contains comments . Writing comments in code is a good programming habit . It helps other programmers read your code , Understand the function of each part of the program file or script . therefore , You can add comments to the script like this :
#!/usr/bin/awk -f
# How is this in awk An example of writing comments in
# Use 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 get the account file /etc/passwd Find a person named aaronkilik Users of , Then print the user name like this 、 User ID、 User GID :
The following is the content of our script file , The file named second.awk.
#! /usr/bin/awk -f
# Use BEGIN Specify characters to set FS Built-in variables
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 , Then execute it like this :
$ chmod +x second.awk
$ ./second.awk /etc/passwd
sample output :
Username : aaronkilik User ID : 1000 User GID : 1000
In the last example below , We will use do while Statement to print numbers 0-10:
The following is the content of our script file , The file named do.awk.
#! /usr/bin/awk -f
#printing from 0-10 using a do while statement
#do while statement
BEGIN {
#initialize a counter
x=0
do {
print x;
x+=1;
}while(x<=10)
}
After saving the file , Make the script executable as before . then , Run it :
$ chmod +x do.awk
$ ./do.awk
sample output
0
1
2
3
4
5
6
7
8
9
10
summary
We have reached this wonderful awk The end of the series , I hope you have learned a lot from past articles , Think of these as you awk Introduction to programming language . I mentioned it from the beginning ,awk Is a complete text processing language , So you can learn a lot awk Other aspects of programming languages , For example, environment variables 、 Array 、 function ( Built in or user-defined ), wait .
边栏推荐
- Flutter 集成 amap_flutter_location
- Developers really review CSDN question and answer function, and there are many improvements~
- 小程序容器技术与物联网 IoT 可以碰撞出什么样的火花
- 2022 gas examination registration and free gas examination questions
- What does range mean in PHP
- 根据数字显示中文汉字
- The basic syntax of mermaid in typera
- Cannot click button when method is running - C #
- What sparks can applet container technology collide with IOT
- 团体程序设计天梯赛-练习集 L1-006 连续因子
猜你喜欢
Four essential material websites for we media people to help you easily create popular models
Turn: excellent managers focus not on mistakes, but on advantages
09 softmax regression + loss function
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Educational Codeforces Round 119 (Rated for Div. 2)
System disk expansion in virtual machine
Take you to master the formatter of visual studio code
How can we make a monthly income of more than 10000? We media people with low income come and have a look
1. Getting started with QT
随机推荐
Démarrage des microservices: passerelle
Comparison between sentinel and hystrix
ES6 summary
ctfshow web255 web 256 web257
Xcode 6 swift code completion does not work properly - Xcode 6 swift code completion not working
Call Baidu map to display the current position
转:优秀的管理者,关注的不是错误,而是优势
ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
Li Kou today's question -1200 Minimum absolute difference
一文了解數據异常值檢測方法
随机事件的关系与运算
Manjaro install wechat
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
AI Winter Olympics | is the future coming? Enter the entrance of the meta universe - virtual digital human
Four essential material websites for we media people to help you easily create popular models
Educational Codeforces Round 119 (Rated for Div. 2)
awk从入土到入门(10)awk内置函数
Unity write word
FOC control
Difference between static method and non static method (advantages / disadvantages)