当前位置:网站首页>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 编程语言的其它方面,例如环境变量、数组、函数(内置的或者用户自定义的),等等。
边栏推荐
- SQL statement view SQL Server 2005 version number
- Wechat has new functions, and the test is started again
- How to use C language code to realize the addition and subtraction of complex numbers and output structure
- 没有Kubernetes怎么玩Dapr?
- How to re enable local connection when the network of laptop is disabled
- Moher College phpmailer remote command execution vulnerability tracing
- [performance test] read JMeter
- 1. Getting started with QT
- 09 softmax regression + loss function
- What if the wireless network connection of the laptop is unavailable
猜你喜欢

C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码

Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service

Newh3c - routing protocol (RIP, OSPF)

Go h*ck yourself:online reconnaissance (online reconnaissance)

DM8 tablespace backup and recovery

ctfshow web255 web 256 web257

1. Getting started with QT

snipaste 方便的截图软件,可以复制在屏幕上

Chrome is set to pure black

Guanghetong's high-performance 4g/5g wireless module solution comprehensively promotes an efficient and low-carbon smart grid
随机推荐
Manjaro install wechat
力扣今日题-1200. 最小绝对差
团体程序设计天梯赛-练习集 L1-006 连续因子
MySQL relearn 1-centos install mysql5.7
DM database password policy and login restriction settings
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码
Moher college phpMyAdmin background file contains analysis traceability
ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
运动【跑步 01】一个程序员的半马挑战:跑前准备+跑中调整+跑后恢复(经验分享)
NPM run build error
System disk expansion in virtual machine
Example analysis of C # read / write lock
NewH3C——ACL
What if the wireless network connection of the laptop is unavailable
微服务入门:Gateway网关
High order phase difference such as smear caused by myopic surgery
Scanf read in data type symbol table
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
How can we make a monthly income of more than 10000? We media people with low income come and have a look