awk编程总结

来源:互联网 发布:淘宝模特兼职价格 编辑:程序博客网 时间:2024/06/07 09:03

注意:如果以命令行的方式提供awk程序,而不是将它写入一个文件中,那么在程序的任何地方都不能出现单引号,否则shell将对它进行解释而导致错误。

记录:每一行输入为一个记录。

字段: 由空格或制表符分隔的单词成为字段。

使用-F来修改分隔符,例如:

-F,

将分隔符修改成逗号。

常用系统变量:

FS Field separator (default=whitespace)
RS Record separator (default=\n)
NF Number of fields in current record
NR Number of the current record
OFS Output field separator (default=space)
ORS Output record separator (default=\n)
FILENAME Current filename

简单范例:

awk '$2 !~ /E/{print $1, $2}' datafile
awk '/^[ns]/{print $1}' datafile
awk '$3 * $4 > 500 {print $0}' file
awk '$1 ~ /Tom/{wage = $3 * $4; print wage}' filename

内建函数:

tolower(string) 
toupper(string) 
index(s, t) 
length(s) 
split(string, array, fieldsep) 
substr(s,p) substr(s,p,n)
match(s,r)

控制结构语句:

if ( NR < 3 )print $2elseprint $3for (i = 1; i <= NR; i++){      total += $i      count++}i = 1while (i <= NF){      print i, $i      i++}i = 1 do {   print $0   i++} while (i <= 10)


0 0
原创粉丝点击