当前位置:网站首页>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 编程语言的其它方面,例如环境变量、数组、函数(内置的或者用户自定义的),等等。
边栏推荐
- Li Kou today's question -1200 Minimum absolute difference
- string. Format without decimal places will generate unexpected rounding - C #
- 如何通过antd的upload控件,将图片以文件流的形式发送给服务器
- 【Go基础】1 - Go Go Go
- Take you to master the formatter of visual studio code
- Question 49: how to quickly determine the impact of IO latency on MySQL performance
- Go h*ck yourself:online reconnaissance (online reconnaissance)
- [performance test] read JMeter
- 微服务入门:Gateway网关
- C#实现一个万物皆可排序的队列
猜你喜欢
Developers really review CSDN question and answer function, and there are many improvements~
What should I do if there is a problem with the graphics card screen on the computer
go-zero微服务实战系列(九、极致优化秒杀性能)
Question 49: how to quickly determine the impact of IO latency on MySQL performance
1、卡尔曼滤波-最佳的线性滤波器
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
Conversion of yolov5 XML dataset to VOC dataset
The second session of the question swiping and punching activity -- solving the switching problem with recursion as the background (I)
DM8 uses different databases to archive and recover after multiple failures
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
随机推荐
Convert datetime string to datetime - C in the original time zone
Basic operations of databases and tables ----- view data tables
学习Nuxt.js
微服务入门:Gateway网关
广和通高性能4G/5G无线模组解决方案全面推动高效、低碳智能电网
How to play dapr without kubernetes?
Leetcode 146. LRU cache
What if I forget the router password
R language ggplot2 visualization: ggplot2 visualization grouping box diagram, place the legend and title of the visualization image on the top left of the image and align them to the left, in which th
1. Qt入门
Using the rate package for data mining
2022 gas examination registration and free gas examination questions
SQL statement view SQL Server 2005 version number
A method for detecting outliers of data
Flutter integrated amap_ flutter_ location
ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
DM8 database recovery based on point in time
DM8 tablespace backup and recovery
ctfshow web255 web 256 web257
[attack and defense world | WP] cat