示例文件:

[root@orclsrv ~]# catsample Heigh-ho! sing,heigh-ho! unto the green holly:Most friendship isfeigning, most loving mere folly:Then, heigh-ho, theholly!

使用感叹号(!) 作为字段分隔符(FS)打印示例数据的第1 个字段:

[root@orclsrv~]#  awk ' BEGIN { FS = "!" } {print $1 } ' sampleHeigh-hoMost friendship isfeigning, most loving mere folly:Then, heigh-ho, theholly

但是字段分隔符并不一定必须是单个字符。可以使用一个短语:

[root@orclsrv ~]# awk' BEGIN { FS = "Heigh-ho" } { print $2 } ' sample! sing, heigh-ho!unto the green holly:

 

下面的示例将匹配短语,无论大小写:

[root@orclsrv ~]# awk' BEGIN { FS = "[Hh]eigh-ho" } { print $2 } ' sample! sing,  , the holly!

将记录分隔符(RS)更改为逗号,并对示例文件使用它:(一行称之为记录,所有以逗号结尾将换行)

[root@orclsrv ~]# awk' BEGIN { RS = "," } //' sampleHeigh-ho! sing heigh-ho! unto the green holly:Most friendship isfeigning most loving mere folly:Then heigh-ho the holly!

输出记录分隔符(ORS)

[root@orclsrv ~]# awk'BEGIN {ORS=""} //' sampleHeigh-ho! sing,heigh-ho! unto the green holly:Most friendship is feigning, most loving me

 

输出记录分隔符(ORS)

[root@orclsrv ~]# awk'BEGIN {ORS=""} // { print } END {print "\n"}' sampleHeigh-ho! sing,heigh-ho! unto the green holly:Most friendship is feigning, most loving merefolly:Then, heigh-ho, the holly!

NR(记录的是几行.NR 变量包含当前的记录个数。当读取到第 1 个记录时,其值为 1,当读取到第 2 个记录时,其值增为 2,依此类推。在 END 模式中使用它,以便输出输入中的行数:

[root@orclsrv ~]# awk'END {print NR}' sample 3

如果上面的print语句位于BEGIN模式中,那么该程序将报告其输入包含0 行内容,因为在执行时NR的值为0,因为此时尚未读入任何输入记录:

[root@orclsrv ~]# awk'BEGIN {print NR}' sample 0

使用$NR打印相对于当前记录个数的字段:

[root@orclsrv ~]# awk'{print NR,$NR}' sample 1 Heigh-ho!2 friendship3 the

列出每个记录中字段的个数,以及最后一个字段的值:(NF 变量包含当前记录中字段的个数)

[root@orclsrv ~]# awk' { print "Record " NR " has " NF " fields and endswith " $NF}' sampleRecord 1 has 7 fieldsand ends with holly:Record 2 has 8 fieldsand ends with folly:Record 3 has 4 fieldsand ends with holly!

找出green所在的行:

[root@orclsrv ~]# awk'/green/ {print}' sample Heigh-ho! sing,heigh-ho! unto the green holly:

包含两个感叹号,并且其中可以有任意数量的文本:

[root@orclsrv ~]# awk'/!.*!/' sample Heigh-ho! sing,heigh-ho! unto the green holly:

使用~操作符,这表示包含

仅打印示例数据中第3 个字段包含s字符的那些记录:

[root@orclsrv ~]# awk'$3 ~ /s/ ' sample Most friendship isfeigning, most loving mere folly:

 

打印那些登录Shell 不是bash的所有用户的全名:

[root@orclsrv ~]# awk'BEGIN {FS=":"} $7 !~ /bash/ {print $5}' /etc/passwdbindaemonadmlp…TomcatHAL daemonavahi-autoipd Sabayon user

 

输出示例数据中匹配regexpintheto的那些记录的第4 个字段:

[root@orclsrv ~]# awk'/in/ && /the/ && /to/ {print $4}' sample Unto

打印示例数据中不包含holly确实包含most的那些记录,同时将输出记录分隔符更改为连字符:

[root@orclsrv ~]# awk'BEGIN {OFS="-"} !/holly/ || /most/ {print $1,$2}' sample Most-friendship

 

在两个模式之间使用逗号,可以指定一个范围,这表示匹配位于这两种模式之间和模式本身的所有文本, 范围模式在匹配文本时可以跨越不同的记录。它输出包含匹配项部分内容的完整的记录。例如,搜索从Heighfolly的范围,将输出前两个记录的文本:

[root@orclsrv ~]# awk'/Heigh/,/folly/' sample Heigh-ho! sing,heigh-ho! unto the green holly:Most friendship isfeigning, most loving mere folly:

 

如果您使用换行符作为字段分隔符、使用空字符串作为记录分隔符,那么 AWK 会将整段内容作为一个记录。这样使其成为了段落grep,它将输出匹配搜索的整段内容。对于包含大量文本的文件,该功能可能非常有价值。

重定义分隔符,然后在示例文件中搜索 green。如果不进行重新定义,该搜索仅输出第 1 个记录(即第 1 行)。但是,因为整个示例文件中只有一个段落,所以该匹配将输出整个文件本身:

[root@orclsrv ~]# awk 'BEGIN {FS="\n"; RS="" } /green/' sampleHeigh-ho! sing,heigh-ho! unto the green holly:Most friendship isfeigning, most loving mere folly:Then, heigh-ho, theholly!

参考网址:http://www.ibm.com/developerworks/cn/education/aix/au-gawk/index.html