数组与split函数: awk的内置函数split能够将字符串拆分为词,然后保存在数组中。您可以指定字段分隔符,也可以就用FS的当前值。
格式
split(字符串,数组,字段分隔符)
split(字符串,数组)
split(字符串,数组)
范例
$ awk 'BEGIN{split("3/15/2015",date,"/");\
print "The month is " date[1] " and the year is "date[3] "}' filename
The month is 3 and the year is 2015.
print "The month is " date[1] " and the year is "date[3] "}' filename
The month is 3 and the year is 2015.
说明:将字符串3/15/2015保存到数组date中,用正斜杠作为字段分隔符。现在date[1]中是3,date[2]中是15,而date[3] 中则是2015。字段分隔符用第3个参数指定,如未指定,就以FS的值做字段分隔符。
delete函数:delete函数用于删除数组元素。
范例:
$ awk '{line[x++]=$2}END{for(x in line) delete(line[x])}' filename
说明:赋给数组line的值是第2个字段的值。所存记录都处理完后,特殊for循环将遍历数组的所有元素,并由delete函数来删除它们。