Shell 编程的老臣 - gawk

shell 的世界里有两个好兄弟:sed 和 gawk. 今天讲 gawk.

awk 简史:

The name awk comes from the initials of its designers: Alfred V. Aho, Peter J. Weinberger, and Brian W. Kernighan. The original version of awk was written in 1977 at AT&T Bell Laboratories.

gawk 简史:

Paul Rubin wrote gawk in 1986. Jay Fenlason completed it, with advice from Richard Stallman. John Woods contributed parts of the code as well. In 1988 and 1989, David Trueman, thoroughly reworked gawk for compatibility with the newer awk.

gawk 是 awk 的 GNU 版本。是一个功能更加强大的具有编程扩展性的工具。

gawk 的命令格式

gawk options program file

简单的对 options 做个介绍:

-F fs : 指定航中分割数据字段的字段分隔符

-f file: 指定读取程序的文件名。多行命令可以放在一个文件以便复用

-v var=value:   指定 gawk 程序中一个变量以及其默认值
-mf N:  指定要处理的数据文件中的最大字段数
-mr N:  指定数据文件中的最大数据行数
-W keyword: 指定 gawk 的兼容模式或警告等级

gawk 的命令编程

gawk 的中心是其命令,可以有两种方式来调用命令

命令行的调用方式;

将多行命令编写在文件的调用方式

命令行的调用方式:
[root@centos00 _data]# gawk '{print "hello, world!"} '
_
要注意的是两点:

‘{}’ 成为 gawk 的固定格式,{} 是放置 gawk 命令的地方,而” 是将命令当做字符串与其他选项或参数字符串隔离的分隔符。

[root@centos00 _data]# gawk 'print "hello, world!" '
gawk: cmd. line:1print "hello, world!" 
gawk: cmd. line:1: ^ syntax error
[root@centos00 _data]# gawk {print "hello, world!"}
bash: !"}: event not found
[root@centos00 _data]#

gawk 的默认从标准输入流来读文本,如果没有指定文本文件,那就等待标准输入流的输入:

[root@centos00 _data]# gawk '{print "hello, world!"} '
this a hello world programm
hello, world!

多条命令也可以写在一行中处理,使用“;”分隔符即可:

[root@centos00 _data]# gawk -F: '{ $6 = $1 ":" $6 ; print $1 "''s home director is " $6 } ' /etc/passwd
roots home director is root:/root
bins home director is bin:/bin
daemons home director is daemon:/sbin
adms home director is adm:/var/adm
lps home director is lp:/var/spool/lpd
syncs home director is sync:/sbin

对单引号”’”做转义的时候,使用两次单引号即可,而不是使用”\”.

gawk 的功能也是对每行输入做处理。

将多行命令编写在文件的调用方式
[root@centos00 _data]# gawk -F: -f getUserDirectory.awk /etc/passwd
[root@centos00 _data]# cat getUserDirectory.awk
{print $<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">1</span>&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">"'s&nbsp;home&nbsp;directory&nbsp;is&nbsp;"</span>&nbsp;$6 }
[root@centos00 _data]# gawk -F: -f getUserDirectory.awk /etc/passwd
root's home directory is /root
bin'
s home directory is /bin
daemon's home directory is /sbin
adm'
s home directory is /var/adm
lp's home directory is /var/spool/lpd

多行命令写在文件中:

[root@centos00 _data]# cat getUserDirectory.awk
{$6&nbsp;=&nbsp;$1 ":" $6&nbsp;}<br>{<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">print</span>&nbsp;$1&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">"'s&nbsp;home&nbsp;directory&nbsp;is&nbsp;"</span>&nbsp;$6 }

[root@centos00 _data]# gawk -F: -f getUserDirectory.awk /etc/passwd
root's home directory is root:/root
bin'
s home directory is bin:/bin
daemon's home directory is daemon:/sbin

依然是用{} 来引用命令,但不需要”来分割命令字符串了

2,n 是标识被-F 指定的字段分隔符分割的字段值。

$1 就是输入文件中的第一栏

gawk 的重头戏 - “命令”武器库

编程思路大家都没大问题,针对 gawk 首先要了解的是他的武器库,到底藏了哪些宝贝

AK-47,可靠精良,擅长短距离横扫;

M16,轻便成熟,火力猛;

沙漠之鹰,更符合随身细带的要求。

因此依据作战规模,我们需要选好手上的武器:

循环

条件

变量

操作函数

变量

变量又可分为“内建变量”和“自定义变量”

内建变量

举个例子来说明:

[root@centos00 _data]# gawk -F: '{ $6&nbsp;=&nbsp;$1 ":" $6&nbsp;;&nbsp;print&nbsp;$1 "''s home director is " $6 } ' /etc/passwd
roots home director is root:/root
bins home director is bin:/bin
daemons home director is daemon:/sbin

这里的 来标记。而 7 就是第 7 列,且取出来之后,可以对 7 做变更。

那么问题就来了:

是否能将内建变量取出来的值,做修改,再传回源文件做保存呢?

除了 $n ( n 指代 1,2,3,4,5,6…等自然数)之外,还有一些内建变量:

FS: 输入字段分隔符

RS: 输入数据行分隔符

OFS: 输出字段分隔符

ORS: 输出数据行分隔符

[root@centos00 _data]# echo '32:31:33'|gawk 'BEGIN {FS=":";OFS="|"} {print $1,$2,$3}'
32|31|33

ENVIRON 也是比较有意思的内建变量用法:

[root@centos00 _data]# gawk 'BEGIN{ print ENVIRON["PATH"] }'
/root/perl5/bin:/usr/lib64/qt-3.3/bin:/home/huangyun/perl5/bin:/usr/local/bin:
更多 gawk 内建的变量,参考文档:

ftp://ftp.gnu.org/old-gnu/Manuals/gawk-3.0.3/html_chapter/gawk_11.html

https://www.gnu.org/software/gawk/manual/gawk.html#SEC_Contents

自定义变量

无法想象只有内建变量的编程世界会是怎么样,大概会像少了插件那样去用 visual studio code 吧,很无助。所以自定义变量是肯定会支持的

[root@centos00 _data]# gawk '{Greetings="hello world";print Greetings}'
d
hello world
^C
[root@centos00 _data]# gawk 'BEGIN{Greetings="hello world";print Greetings}'
hello world
[root@centos00 _data]#

Greetings 是自定义的变量,第一个例子很有趣,没有BEGIN 指令,gawk 始终是在等待输入。

自定义变量之数组

数组在 gawk 中的使用,更像是 K-V 对:

[root@centos00 _data]# gawk 'BEGIN{
Prize["One"]="house"
Prize["Two"]="iphoneX"
for( prize in Prize)
{
  print Prize[prize]
}
}'
iphoneX
house

变量在程序结构中是区分大小写的!是区分大小写的!是区分大小写的!

结构化命令 (条件,循环)
if 条件:

if(condition)
 statement
# while 循环

while(condition)
{
    statements
}
do while 循环

do
{
    statements
}
while(condition)
# for 循环 

# c语言风格
for(i=1;i<4;i++)
{
    total += $i
}

# python 式

for( prize in Prize)
{
  print Prize[prize]
}

已知每行字段总数可以用内建变量 NF 标识,据此可以打印出每个字段的值:

[root@centos00 _data]# cat testData.txt
123 345 567 789
111 222 333 444
333 555 666 777
[root@centos00 _data]# gawk '{for(i=1;i<=NF;i++){ print $i } }' testData.txt
123
345
567
789
111
222
333
444
333
555
666
777
[root@centos00 _data]#
操作函数

跟变量一样,可分“内建函数”和“自定义函数”

内建函数参考文档即可,不需要太多的解释。

我们该关心的还是自定义函数。

自定义函数的格式

[root@centos00 _data]# gawk '
> function getRand(myRand)
> {
> print myRand*rand()
> }
> BEGIN{
> print getRand(4)
> }'
0.95115

[root@centos00 _data]#

所有的自定义函数一定是放在 BEGIN 之前,或者说程序的开头。

但马上就会有问题的是,函数必须重用。重复发明不必要的轮子,是低效的。因此 gawk 给出了函数库这个概念。

将所有的自定义函数归档到函数库中,在使用个别函数时候,只要引用这个库就可以了。相信 Java 朋友不陌生,Python 朋友简直是秒懂,就是库引用嘛!

[root@centos00 _data]# cat funclib.awk
function myprint()
{
    printf "%-16s - %s\n", $1,&nbsp;$4
}

function getRand(myRand)
{
    print myRand*rand()
}


[root@centos00 _data]# cat rand.awk
BEGIN{FS=":"}
{
    getRand(NF)
}

[root@centos00 _data]# gawk -f funclib.awk -f rand.awk /etc/passwd
1.66451
2.03746
5.9207
1.06546

AWK是一种优良的文本处理工具。它不仅是 Linux 也是任何环境现有的功能最强大的数据处理引擎之一。这种编程及数据操作语言(其名称得自于它的创始人 Alfred Aho 、Peter Weinberger 和 Brian Kernighan 姓氏的首个字母)的最大功能取决于一个人所拥有的知识。AWK 提供了极其强大的功能:可以进行样式装入、流控制、数学运算符、进程控制语句甚至于内置的变量和函数。它具备了一个完整的语言所应具有的几乎所有精美特性。实际上 AWK 的确拥有自己的语言:AWK 程序设计语言, 三位创建者已将它正式定义为“样式扫描和处理语言”。它允许您创建简短的程序,这些程序读取输入文件、为数据排序、处理数据、对输入执行计算以及生成报表,还有无数其他的功能。 最简单地说, AWK 是一种用于处理文本的编程语言工具。AWK 在很多方面类似于 shell 编程语言,尽管 AWK 具有完全属于其本身的语法。它的设计思想来源于 SNOBOL4 、sed 、Marc Rochkind设计的有效性语言、语言工具 yacc 和 lex ,当然还从 C 语言获取了一些优秀的思想。在最初创造 AWK 时,其目的是用于文本处理,并且这种语言的基础是,只要在输入数据有模式匹配,就执行一系列指令。该实用工具扫描文件的每一行,查找与命令行所给定内容相匹配的模式。如果发现匹配内容,则进行下一个编程步骤。如果找不到匹配内容,则继续处理下一行。 尽管操作可能会很复杂,但命令的语法始终是: awk '{pattern + action}' 或者 awk 'pattern {action}' 其 pattern 表示 AWK 在数据查找的内容,而 action 是在找到匹配内容时所执行的一系列命令。花括号 ({}) 不需要在程序始终出现,但它们用于根据特定的模式对一系列指令进行分组。 gawk 是 AWK 的 GNU 版本。 一般的UNIX作业系统,本身即附有AWK,不同的UNIX作业系统所附的AWK其版本亦不尽相同,若读者所使用的系统上未附有AWK,可通过 anonymous ftp 到下列地方取得: phi./pub/gnu ftp/UNIX/gnu preppub/gnu 注 解:一种编程语言,因其模式匹配语法而特别有用,通常用于数据检索和数据转换。一个GNU版本称为Gawk
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dbLenis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值