awk一些函数+简单示例,没有概念,实践中记忆:
1.split //split(域名,数组,分隔符)
[root@localhost ~]# cat a
abc abd abe abf abg
[root@localhost ~]# awk '{split($0,a," "); print length(a);for(i in a) print a[i]}' a //$0按空格作为分隔符,顺序存入数组a中
5
abf
abg
abc
abd
abe
2.sub //替换函数,sub(旧字符串,新字符串,作用域)
[root@localhost ~]# echo abc abc abc abc | awk '{sub("c","k",$4); print }' //把第4个作用域的c替换为k
abc abc abc abk
[root@localhost ~]# echo abc abc abc abc | awk '{sub("c","k"); print }' //没写作用域,默认匹配第一次
abk abc abc abc
3.gsub //相比sub,多了global参数
[root@localhost ~]# echo abc abc abc abc | awk '{gsub("c","k"); print }'
abk abk abk abk
[root@localhost ~]# echo abc abc abc abc | awk '{gsub("c","k",$4); print }'
abc abc abc abk
4.index //index(作用域,字符串)
[root@localhost ~]# echo abc ack | awk '{if(index($1,"k")) print "yes";else print "no"; }'
no
[root@localhost ~]# echo abc ack | awk '{if(index($2,"k")) print "yes";else print "no"; }'
yes
[root@localhost ~]# echo abc ack | awk '{print index($2,"k")}' //查到匹配字符串位置
3
5.length(s) //打印字符串长度
[root@localhost ~]# echo abcdefg hijkl | awk '{ print length($0) }'
13
6.match //match(作用域,字符串) 跟index类似
[root@localhost ~]# echo abc abk | awk '{ print match($2,"k") }'
3
[root@localhost ~]# echo abc abk | awk '{ print match($1,"k") }'
0
7.substr //截取字符串,substr(作用域,开始位置,结束位置)
[root@localhost ~]# echo abc abchelloworld | awk '{b=substr($2,4); print b}' //没有结束位置,默认到结尾
helloworld
[root@localhost ~]# echo abc abchelloworld | awk '{b=substr($2,4,5); print b}' //第4个字符开始取出5个字符
hello
6.tolower(s) //字符串转换为小写
[root@localhost ~]# echo ABC ABC ABC | awk '{print tolower($0)}'
abc abc abc
[root@localhost ~]# echo ABC ABC ABC | awk '{b=tolower($2); print b}'
abc
7.toupper(s) //字符串转换为大写
[root@localhost ~]# echo abc abc abc | awk '{print toupper($0)}'
ABC ABC ABC
[root@localhost ~]# echo abc abc abc | awk '{b=toupper($2); print b}'
ABC
8.system() //执行系统命令
[root@localhost ~]# awk 'BEGIN{ print system("ls -a") }'
. .bash_logout .dmrc .gnome .ICEauthority .mysql_history .Trash
.. .bash_profile .eggcups .gnome2 install.log .nautilus .viminfo
a .bashrc .evolution .gnome2_private install.log.syslog .redhat .Xauthority
anaconda-ks.cfg .cshrc .gconf .gstreamer-0.10 .metacity .tcshrc
.bash_history Desktop .gconfd .gtkrc-1.2-gnome2 .mozilla .thumbnails
0
9.gensub() //gensub(旧字符串,新字符串,替换位置,作用域)
[root@localhost ~]# echo abaa adaa aaaa | awk '{b=gensub("a","k",3,$3); print b}'
aaka
[root@localhost ~]# echo abaa adaa aaaa | awk '{b=gensub("a","k",4,$3); print b}'
aaak
10.strftime() //时间函数
[root@localhost ~]# awk 'BEGIN{ now=strftime("%y-%m-%d %H-%M-%S"); print now}'
13-01-12 21-14-30
getline //获取下一行,以下几种常用用法
[root@localhost ~]# echo -e "abc\nabd\nabe" > a
[root@localhost ~]# awk 'BEGIN{ getline < "a"; print }'
abc
[root@localhost ~]# awk '/abc/{ getline tmp; print tmp; print }' a //当前行和下一行对调
abd
abc
[root@localhost ~]# awk 'BEGIN { getline < "-"; print }' //打印键盘输入的内容
hello
hello
[root@localhost ~]# awk '{ getline line < "a"; print line }' a //打印出文件内容
abc
abd
abe
[root@localhost ~]# seq 10 | awk '{getline; print}' //注与BEGIN的区别,此处awk指针有跳转
2
4
6
8
10
[root@localhost ~]# seq 10 | awk 'BEGIN{while(getline)print}' //此处awk指针没有跳转
1
2
3
4
5
6
7
8
9
10
next //结束当前行,读取下一行,并从从头执行命令
[root@localhost ~]# cat a
abc
abd
abe
[root@localhost ~]# awk '/abc/{print "yes"; next;}{ print "no"}' a
yes
no
no
exit //退出执行
[root@localhost ~]# echo -e "a\na\na\na\na\na\nb\na\na" | awk '/b/{exit;}{print "yes"}'
yes
yes
yes
yes
yes
yes