当前位置:网站首页>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 .
边栏推荐
- What should I do if there is a problem with the graphics card screen on the computer
- PHP converts seconds to timestamps - PHP
- DM8 tablespace backup and recovery
- The upper layer route cannot Ping the lower layer route
- OpenFeign 服务接口调用
- The right way to capture assertion failures in NUnit - C #
- Educational Codeforces Round 119 (Rated for Div. 2)
- 学习Nuxt.js
- 埃氏筛+欧拉筛+区间筛
- Comparison between sentinel and hystrix
猜你喜欢
Mouse over to change the transparency of web page image
What should I do if there is a problem with the graphics card screen on the computer
DM8 command line installation and database creation
根据数字显示中文汉字
[error record] no matching function for call to 'cacheflush' cacheflush();)
How to play dapr without kubernetes?
Guanghetong's high-performance 4g/5g wireless module solution comprehensively promotes an efficient and low-carbon smart grid
Industry depression has the advantages of industry depression
Newh3c - routing protocol (RIP, OSPF)
Codeforces Round #803 (Div. 2)(A-D)
随机推荐
Parallel shift does not provide any acceleration - C #
Convert datetime string to datetime - C in the original time zone
Three paradigms of database design
snipaste 方便的截图软件,可以复制在屏幕上
Bishi blog (13) -- oral arithmetic test app
How to pass custom object via intent in kotlin
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
力扣今日题-1200. 最小绝对差
The right way to capture assertion failures in NUnit - C #
Newh3c - network address translation (NAT)
2022 electrician (intermediate) examination question bank and electrician (intermediate) examination questions and analysis
awk从入门到入土(4)用户自定义变量
Sports [running 01] a programmer's half horse challenge: preparation before running + adjustment during running + recovery after running (experience sharing)
Démarrage des microservices: passerelle
Group programming ladder race - exercise set l2-002 linked list de duplication
From scratch, use Jenkins to build and publish pipeline pipeline project
NewH3C——ACL
ArcGIS application (XXII) ArcMap loading lidar Las format data
4 small ways to make your Tiktok video clearer
C#实现一个万物皆可排序的队列