当前位置:网站首页>awk从入门到入土(12)awk也可以写脚本,替代shell
awk从入门到入土(12)awk也可以写脚本,替代shell
2022-07-04 08:38:00 【奇妙之二进制】
概述
和写 shell 脚本差不多,首先第一行声明解释器路径,例如在我的系统上,awk 工具安装在 /user/bin/awk 目录,所以我的 awk 脚本以如下内容作为开头:
#! /usr/bin/awk -f
上面一行的解释如下:
- #! ,称为释伴,指明使用那个解释器来执行脚本中的命令;
- /usr/bin/awk ,即解释器;
- -f ,解释器选项,用来指定读取的程序文件;
说是这么说,现在从下面的简单例子开始,让我们深入研究一些可执行的 awk 脚本。使用你最喜欢的编辑器创建一个新文件,像下面这样:
$ vi script.awk
然后把下面代码粘贴到文件中:
#!/usr/bin/awk -f
BEGIN { printf "%s\n","Writing my first awk executable script!" }
保存文件后退出,然后执行下面命令,使得脚本可执行:
$ chmod +x script.awk
然后,执行它:
$ ./script.awk
输出样例:
Writing my first awk executable script!
添加注释
一个严格的程序员一定会问:“注释呢?”。是的,你可以在 awk 脚本中包含注释。在代码中写注释是一种良好的编程习惯。它有利于其它程序员阅读你的代码,理解程序文件或者脚本中每一部分的功能。所以,你可以像下面这样在脚本中增加注释:
#!/usr/bin/awk -f
# 这是如何在 awk 中写注释的示例
# 使用特殊模式 BEGIN 来输出一句话
BEGIN { printf "%s\n","Writing my first awk executable script!" }
接下来我们看一个读文件的例子。我们想从帐号文件 /etc/passwd 中查找一个叫 aaronkilik 的用户,然后像下面这样打印用户名、用户的 ID、用户的 GID :
下面是我们脚本文件的内容,文件名为 second.awk。
#! /usr/bin/awk -f
# 使用 BEGIN 指定字符来设定 FS 内置变量
BEGIN { FS=":" }
# 搜索用户名 aaronkilik 并输出账号细节
/aaronkilik/ { print "Username :",$1,"User ID :",$3,"User GID :",$4 }
保存文件后退出,使得脚本可执行,然后像下面这样执行它:
$ chmod +x second.awk
$ ./second.awk /etc/passwd
输出样例:
Username : aaronkilik User ID : 1000 User GID : 1000
在下面最后一个例子中,我们将使用 do while 语句来打印数字 0-10:
下面是我们脚本文件的内容,文件名为 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)
}
保存文件后,像之前操作一样使得脚本可执行。然后,运行它:
$ chmod +x do.awk
$ ./do.awk
输出样例
0
1
2
3
4
5
6
7
8
9
10
总结
我们已经到达这个精彩的 awk 系列的最后,我希望你从过往的文章中学到了很多知识,把这些当作你 awk 编程语言的入门指导。从一开始就提到过,awk 是一个完整的文本处理语言,所以你可以学习很多 awk 编程语言的其它方面,例如环境变量、数组、函数(内置的或者用户自定义的),等等。
边栏推荐
- Codeforces Round #793 (Div. 2)(A-D)
- @Role of requestparam annotation
- Comparison between sentinel and hystrix
- Comprendre la méthode de détection des valeurs aberrantes des données
- FOC控制
- 微服务入门:Gateway网关
- If the array values match each other, shuffle again - PHP
- FRP intranet penetration, reverse proxy
- ctfshow web255 web 256 web257
- Internal learning
猜你喜欢
How to play dapr without kubernetes?
NewH3C——ACL
How to solve the problem that computers often flash
User login function: simple but difficult
Codeforces Round #803 (Div. 2)(A-D)
09 softmax regression + loss function
Redis sentinel mechanism
DM8 tablespace backup and recovery
std::is_ union,std::is_ class,std::integral_ constant
Conversion of yolov5 XML dataset to VOC dataset
随机推荐
High order phase difference such as smear caused by myopic surgery
力扣今日题-1200. 最小绝对差
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
MySQL relearn 1-centos install mysql5.7
What does range mean in PHP
Leetcode 146. LRU cache
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
@Role of requestparam annotation
Unity-Text上标平方表示形式+text判断文本是否为空
How to get bytes containing null terminators from a string- c#
Show server status on Web page (on or off) - PHP
What if I forget the router password
Moher College phpmailer remote command execution vulnerability tracing
广和通高性能4G/5G无线模组解决方案全面推动高效、低碳智能电网
一文了解數據异常值檢測方法
Educational Codeforces Round 115 (Rated for Div. 2)
Example analysis of C # read / write lock
Four essential material websites for we media people to help you easily create popular models
Learn nuxt js
Manjaro install wechat