当前位置:网站首页>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 编程语言的其它方面,例如环境变量、数组、函数(内置的或者用户自定义的),等等。
边栏推荐
- How to choose solid state hard disk and mechanical hard disk in computer
- A single element in an ordered array
- What if I forget the router password
- Manjaro install wechat
- How to improve your system architecture?
- Basic operations of databases and tables ----- view data tables
- C # implements a queue in which everything can be sorted
- [CV] Wu Enda machine learning course notes | Chapter 9
- C#实现一个万物皆可排序的队列
- Show server status on Web page (on or off) - PHP
猜你喜欢

埃氏筛+欧拉筛+区间筛

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

Four essential material websites for we media people to help you easily create popular models

L1 regularization and L2 regularization

DM8 uses different databases to archive and recover after multiple failures

Openfeign service interface call

Snipaste convenient screenshot software, which can be copied on the screen

广和通高性能4G/5G无线模组解决方案全面推动高效、低碳智能电网

OpenFeign 服务接口调用

Redis 哨兵机制
随机推荐
1、卡尔曼滤波-最佳的线性滤波器
How to get bytes containing null terminators from a string- c#
Using the rate package for data mining
Moher College webmin unauthenticated remote code execution
ctfshow web255 web 256 web257
ctfshow web255 web 256 web257
DM database password policy and login restriction settings
How to use C language code to realize the addition and subtraction of complex numbers and output structure
Flutter 集成 amap_flutter_location
yolov5 xml数据集转换为VOC数据集
deno debugger
[test de performance] lire jmeter
Learn nuxt js
Moher college phpMyAdmin background file contains analysis traceability
System disk expansion in virtual machine
Educational Codeforces Round 119 (Rated for Div. 2)
Redis sentinel mechanism
Openfeign service interface call
PHP converts seconds to timestamps - PHP
DM8 database recovery based on point in time