多条件逻辑:&& ||
匹配://纯字符匹配 !//纯字符不匹配 ~//字段值匹配 !~//字段值不匹配 ~/a1|a2/字段值匹配a1或a2
模式 pattern{action}
/regular expression/
relational expression
BEGIN
END
pattern, pattern
数组表达式
array[index] = value
for (item in array)
if (index in array)
算术函数
1. 生成随机数函数:rand()、srand(x)
2. 整数函数:int(x)
3. 三角函数:sqrt(x)、cos(x)、sin(x)、atan2(y,x)、 log(x)、exp(x)
字符串函数(10个)
1. 分割字符串:split(s,a,sep)
2. 格式化函数:sprintf("fmt", expr)
3. 子串函数:index(s,t)、substr(s,p,n)
4. 字符串长度:length(s)
5. 替换函数:sub(r,s,t)、gsub(r,s,t)
6. 大小写转换: tolower(s)、 toupper(s)
7. match函数:match(s,r)
“底部抽屉”函数:getline函数、close()、system()
注意:getline与next的区别。
控制语句: exit、next
控制语句: if、for、while、do while、break、continue
输出函数:printf、sprintf
%-width.precision format-specifier
awk执行shell命令
[root@localhost html]# echo "abc"|awk '{cmd="ls $1";system(cmd);}'
50x.html i.ip index.html
[root@localhost html]# echo "abc"|awk '{cmd=sprintf("echo %s",$1);system(cmd);}'
abc
base64加密
[root@localhost html]# echo "attytbc"|awk '{cmd=sprintf("echo -n %s|base64",$1);system(cmd);}'
YXR0eXRiYw==
[root@localhost html]# echo "YXR0eXRiYw=="|awk '{cmd=sprintf("echo -n %s|base64 -d",$1);system(cmd);}'
attytbc[root@localhost html]#
按位操作
awk中没有左移、右移、按位取反、按位与、按位或、按位异或的操作符,只能用函数。
lshift(val, count)、rshift(val, count)、compl(val)、and(v1, v2)、or(v1, v2)、xor(v1, v2)。
如将字符串ip转换成数值ip:
[root@localhost html]# cat i.ip
192.168.1.107
[root@localhost html]# cat i.ip |awk 'BEGIN{FS="."}{ip1=lshift($1,24);ip2=lshift($2,16);ip3=lshift($3,8);ip4=$4;ip=ip1+ip2+ip3+ip4;print ip}'
3232235883
再将数值ip转换为字符串ip:
[root@localhost html]# cat i.ip
3232235883
[root@localhost html]# cat i.ip|awk '{ip1=rshift(and($1,0xff000000),24);ip2=rshift(and($1,0xff0000),16);ip3=rshift(and($1,0xff00),8);ip4=and($1,0xff);print ip1"."ip2"."ip3"."ip4}'
192.168.1.107