当前位置:网站首页>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 编程语言的其它方面,例如环境变量、数组、函数(内置的或者用户自定义的),等等。
边栏推荐
- Call Baidu map to display the current position
- Snipaste convenient screenshot software, which can be copied on the screen
- 1、卡尔曼滤波-最佳的线性滤波器
- 【性能測試】一文讀懂Jmeter
- Group programming ladder race - exercise set l1-006 continuity factor
- How to solve the problem that computers often flash
- [go basics] 2 - go basic sentences
- Chrome is set to pure black
- Flutter integrated amap_ flutter_ location
- 广和通高性能4G/5G无线模组解决方案全面推动高效、低碳智能电网
猜你喜欢

System disk expansion in virtual machine

What does range mean in PHP

SSRF vulnerability exploitation - attack redis

Developers really review CSDN question and answer function, and there are many improvements~

How to choose solid state hard disk and mechanical hard disk in computer

manjaro安装微信

小程序容器技术与物联网 IoT 可以碰撞出什么样的火花

Fault analysis | MySQL: unique key constraint failure

ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据

Moher college phpMyAdmin background file contains analysis traceability
随机推荐
A method for detecting outliers of data
A single element in an ordered array
@Role of requestparam annotation
How to solve the problem that computers often flash
snipaste 方便的截图软件,可以复制在屏幕上
The upper layer route cannot Ping the lower layer route
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
Moher College phpmailer remote command execution vulnerability tracing
Collections in Scala
一文了解数据异常值检测方法
Put a lantern on the website during the Lantern Festival
没有Kubernetes怎么玩Dapr?
Learn nuxt js
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
1. Kalman filter - the best linear filter
Conversion of yolov5 XML dataset to VOC dataset
If the array values match each other, shuffle again - PHP
Cancel ctrl+alt+delete when starting up
Wechat has new functions, and the test is started again
Cannot click button when method is running - C #