Linux Shell 编程之gawk分析
发布时间:2021-12-10 14:59:12 所属栏目:语言 来源:互联网
导读:ASP站长网 awk 简史: The name awk comes from the initials of its designers: A lfred V. Aho, Peter J. W einberger, and Brian W. K ernighan. The origina
ASP站长网awk 简史: The name awk comes from the initials of its designers: A lfred V. Aho, Peter J. W einberger, and Brian W. K ernighan. 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 : "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 @centos 00 _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 的功能也是对每行输入做处理。 (编辑:焦作站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |