后续应该不会更新awk了,因为不可能把所有的awk用法都写出来,主要还是看工作中的运用。
1. if语句
[root@localhost ~]# awk ‘/root/{i++}END{print i}‘ /etc/passwd 2 [root@localhost ~]# awk -F: ‘{if($3==0){i++}}END{print i}‘ /etc/passwd 1 [root@localhost ~]# awk -F: ‘{if($3!=0){i++}}END{print i}‘ /etc/passwd 30 [root@localhost ~]# awk -F: ‘{if($3>0 && $3<1000){i++}}END{print i}‘ /etc/passwd 30
2. if...else语句
[root@localhost ~]# awk ‘{if($1~1){print "yes"}else {print "no"}}‘ file yes no no no
[root@localhost ~]# awk -F: ‘{if($3==0){i++} else{j++}} END{print i,j}‘ /etc/passwd 1 30 [root@localhost ~]# awk -F: ‘{if($3==0){i++} else{j++}} END{print i"\n"j}‘ /etc/passwd 1 30 [root@localhost ~]# awk -F: ‘{if($3==0){i++} else{j++}} END{print i;print j}‘ /etc/passwd 1 30
3. if...else if...else语句
[root@localhost ~]# awk -F: ‘{if($3==0){i++} else if($3>500){j++} else{k++} } END{print i,j,k}‘ /etc/passwd 1 3 27
[root@localhost ~]# awk -F: ‘{if($3>500) print NR,$1,$3}‘ /etc/passwd 26 www 501 29 zabbix 502 31 nginx 503
4. for/while循环
[root@localhost ~]# awk ‘BEGIN{for(i=1;i<=6;i++) print i}‘ [root@localhost ~]# awk ‘BEGIN{i=1;while(i<=6){print i;i++}}‘ 1 2 3 4 5 6
本文出自 “卡卡西” 博客,请务必保留此出处http://whnba.blog.51cto.com/1215711/1902577
原文:http://whnba.blog.51cto.com/1215711/1902577