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:1: print "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 programmhello, world!

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

[root@centos00 _data]# gawk -F: '{ $6 = $1 ":" $6 ; print $1 "''s home director is " $6 } ' /etc/passwdroots home director is root:/rootbins home director is bin:/bindaemons home director is daemon:/sbinadms home director is adm:/var/admlps home director is lp:/var/spool/lpdsyncs 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/passwdroot's home directory is /rootbin's home directory is /bindaemon's home directory is /sbinadm's home directory is /var/admlp'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/passwdroot's home directory is root:/rootbin's home directory is bin:/bindaemon'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/passwdroots home director is root:/rootbins home director is bin:/bindaemons 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}'dhello 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]}}'iphoneXhouse

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

结构化命令 (条件,循环)
# 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.txt123 345 567 789111 222 333 444333 555 666 777[root@centos00 _data]# gawk '{for(i=1;i<=NF;i++){ print $i } }' testData.txt123345567789111222333444333555666777[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.awkfunction myprint(){    printf "%-16s - %s\n", $1,&nbsp;$4}

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

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

[root@centos00 _data]# gawk -f funclib.awk -f rand.awk /etc/passwd1.664512.037465.92071.06546

Shell 编程的老臣 - gawk相关推荐

  1. shell 编程的老臣 - sed

    vi/vim 的世界里多了两个兄弟:sed 和 gawk. sed: stream editor. 在编辑器处理数据之前,根据事先提供的规则来编辑数据流. sed 有点类似于 Kafka, 对数据进行 ...

  2. shell编程-大杂烩

    ip link显示的数据来源 # ll /sys/class/net lrwxrwxrwx 1 root root 0 Aug 24 10:02 ens3 -> ../../devices/pc ...

  3. Bourne Shell及shell编程

    Bourne Shell及shell编程<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office ...

  4. shell 编程 判断语句参数

    shell 编程中使用到得if语句内判断参数–b 当file存在并且是块文件时返回真 -c 当file存在并且是字符文件时返回真 -d 当pathname存在并且是一个目录时返回真 -e 当pathn ...

  5. Linux Shell 编程学习总结

    Shell 教程 Shell简介:什么是Shell,Shell命令的两种执行方式 Shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成 ...

  6. eq linux_linux之shell编程(二)

    今天给大家继续来分析linux下的shell编程,在上一篇文章简单的介绍了shell编程到底是什么,以及它的一些基本使用规则.我们今天主要来分享shell编程中的一些其他用法,比如说:条件语句,循环语 ...

  7. shell编程之文本处理工具awk

    shell编程之文本处理工具awk 文章目录 shell编程之文本处理工具awk 一.awk介绍 1. awk概述 2. awk能干啥? 二.awk使用方式 1. ==命令行模式使用== ㈠ 语法结构 ...

  8. Windows Shell 编程 第六章 【来源:http://blog.csdn.net/wangqiulin123456/article/details/7987951】...

    第六章 快捷方式的最短路径 Windows Shell允许存储任何对象的引用到系统范围内的任何地点.例如,当你从一个文件夹拖拽可执行程序到另一个文件夹时,鼠标自动改变形状给出除拷贝和移动文件之外的第三 ...

  9. [No000014A]Linux简介与shell编程

    Linux 介绍 内核 库: .so 共享对象,windows:dll 动态链接库 应用程序 Linux的基本原则: 1.由目的单一的小程序组成:组合小程序完成复杂任务: 2.一切皆文件: 3.尽量避 ...

最新文章

  1. Meta 开发 AI 语音助手,用于创建虚拟世界和实时翻译
  2. 用UltraISO制作U盘启动盘,支持windows 7
  3. python个人项目-个人项目 python实现
  4. 042_JDK的Map接口
  5. dotNet中初始化器的使用
  6. hadoop2.4.2集群搭建及hive与mysql集成文档记录
  7. 《Effective C#》Item 20:区分接口实现与虚函数重载
  8. python窗口怎么显示我输入的_Python分别用两个窗口显示和输入
  9. docker-compose搭建EFK,继上篇使用filebeat+es对日志文件的过滤
  10. 百度富文本编辑器UEditor的使用
  11. Code::Blocks安装和汉化包配置
  12. 微信微页面源码H5页面
  13. 原生js倒计时插件封装
  14. 串口之DCB结构体详解
  15. 23种设计模式-依赖倒转原则
  16. linux多系统引导管理,Linux 多重引导MBR与系统引导管理器GRUB.docx
  17. 【Vivado那些事儿】Vivado 增量综合流程
  18. 好好写简历吧!这简历一看就是包装的!!
  19. 【SRS】ATC介绍
  20. C语言学习书籍 零基础入门篇

热门文章

  1. 趣头条深挖市场潜力 探索内容管控新途径
  2. 台州地区十大调查研究咨询机构公司信息
  3. c语言全局变量结构怎么定义,C语言中如何定义全局结构体变量
  4. centos备份远程mysql数据库,CentOS Linux自动备份MySQL数据库到远程FTP服务器并删除指定日期...
  5. 从入局到腾飞 网易云信与在线教育的三年之约
  6. 由浅入深!华为Android面试真题解析,最全的BAT大厂面试题整理
  7. 为什么大厂APP都喜欢做个「极速版」?
  8. Android 7.0 FileProvider 使用说明
  9. s7200液位控制程序_s7-200模拟量
  10. Latex中如何插入矩阵、矩阵方程和方程组