一、print相关实例
[root@localhost ~]#tail -4 /etc/fstab | awk '{print $2,$4}'
[root@localhost ~]#tail -4 /etc/fstab | awk '{print"hello:"$1}'
[root@localhost ~]#tail -4 /etc/fstab | awk '{print"hello:" $1}'
[root@localhost ~]#tail -4 /etc/fstab | awk '{print"hello:$1"}'
[root@localhost ~]#tail -4 /etc/fstab | awk '{print"hello:"$1 1234567}'
[root@localhost ~]#tail -4 /etc/fstab | awk '{print"hello:"$1,1234567}'
二、变量使用实例
-F:等于-v FS=':'
[root@localhost ~]#tail -3 /etc/passwd | awk -F: '{print $1 }'
[root@localhost ~]#tail -3 /etc/passwd | awk -v FS=':' '{print $1 }'
[root@localhost ~]#tail -3 /etc/passwd | awk -v FS=':' '{print $1,$3,$7 }'
[root@localhost ~]#tail -3 /etc/passwd | awk -v FS=':' -v OFS='@' '{print $1,$3,$7 }'
[root@localhost ~]#head -5 /etc/fstab | awk '{print NF}'
[root@localhost ~]#head -5 /etc/fstab | awk '{print $NF}'
[root@localhost ~]#tail -4 /etc/fstab | awk '{print NR}'
[root@localhost ~]#awk '{print FILENAME}' /etc/fstab /etc/issue
[root@localhost ~]#awk 'BEGIN{print ARGC}' /etc/fstab /etc/issue
[root@localhost ~]#awk 'BEGIN{print ARGV[0]}' /etc/fstab /etc/issue
[root@localhost ~]#awk 'BEGIN{print ARGV[1]}' /etc/fstab /etc/issue
[root@localhost ~]#awk 'BEGIN{print ARGV[2]}' /etc/fstab /etc/issue
[root@localhost ~]#awk -v test='hello world' 'BEGIN{print test}'
[root@localhost ~]#awk 'BEGIN{test="hello world";print test}'
三、printf实例
[root@localhost ~]# tail -3 /etc/passwd| awk -F: '{printf "%s",$1}' # 默认在一行显示
[root@localhost ~]#tail -3 /etc/passwd | awk -F: '{printf "%s\n",$1}'
[root@localhost ~]#tail -3 /etc/passwd | awk -F: '{printf "Username:%s\n",$1}'
[root@localhost ~]#tail -3 /etc/passwd | awk -F: '{printf "Username:%s,UID:%d\n",$1,$3}'
[root@localhost~]# head -3 /etc/passwd | awk -F:'{printf "Username: %10s, UID:%d\n",$1,$3}'
[root@localhost~]# head -3 /etc/passwd | awk -F:'{printf "Username: %-10s, UID:%d\n",$1,$3}'
四、条件判断操作符实例
[root@localhost ~]#tail -5 /etc/passwd | awk -F: '{$3>=1000?usertype="CommonUser":usertype="Sysadmin or SysUser";printf"%-15s:%-s\n",$1,usertype}'
五、PATTERN使用实例
[root@localhost ~]#awk '/^UUID/ {print $1}' /etc/fstab
[root@localhost ~]#awk '!/^UUID/ {print $1,$2}' /etc/fstab
[root@localhost ~]#awk -F: '$3>=900{printf "%-30s,%d\n",$1,$3}' /etc/passwd
[root@localhost ~]#awk -F: '$NF=="/bin/bash"{printf "%-20s%s\n",$1,$NF}'/etc/passwd
[root@localhost ~]#awk -F: '$NF~/bash$/{printf"%-20s%s\n",$1,$NF}' /etc/passwd
[root@localhost ~]#awk -F: '(NR>=10&&NR<=15){print $1}' /etc/passwd
[root@localhost ~]#tail -5 /etc/passwd | awk -F: 'BEGIN{print "username uid \n-----------------------------"}{printf"%-20s%d\n",$1,$3}'
[root@localhost ~]#tail -5 /etc/passwd | awk -F: '{print "username uid \n-----------------------------";printf"%-20s%d\n",$1,$3}'
[root@localhost~]# tail -5 /etc/passwd | awk -F:'BEGIN{print "username uid \n-----------------------------"}{printf"%-20s%d\n",$1,$3}END{print"===================\n end"}'
六、if-else语句
[root@localhost ~]#awk -F: '{if($3>=900) print $1,$3}' /etc/passwd
[root@localhost ~]#tail -5 /etc/passwd | awk -F: '{if($3>=1000) {printf "Commonuser:%-34sUid:%d\n",$1,$3} else {printf "Root orSysuser:%-30sUid:%d\n",$1,$3}}'
[root@localhost ~]#awk -F: '{if($NF=="/bin/bash") print $1}' /etc/passwd
[root@localhost ~]#df -h
[root@localhost~]# df -h | awk -F[%] '/^\/dev/{print$1}' | awk '{if($NF>=20) print $1}'
七、while循环
[root@localhost ~]#awk '/^[[:space:]]*linux16/{i=1;while(i<=NF) {print $i,length($i); i++}}'/etc/grub2.cfg
[root@localhost~]# awk'/^[[:space:]]*linux16/{i=1;while(i<=NF) {if(length($i)>=7) {print$i,length($i)};
八、for循环
[root@localhost ~]#awk '/^[[:space:]]*linux16/{for(i=1;i<=NF;i++) {print $i,length($i)}}'/etc/grub2.cfg
九、next
[root@localhost ~]#tail -10 /etc/passwd | awk -F: '{if($3%2!=0) next; print $1,$3}'
十、数组
[root@localhost ~]#netstat -tan | awk '/^tcp\>/{state[$NF]++}END{for(i in state) { printi,state[i]}}'
[root@localhost ~]#awk '/^UUID/{fs[$3]++}END{for(i in fs) {print i,fs[i]}}' /etc/fstab
[root@localhost ~]# awk'{for(i=1;i<=NF;i++){count[$i]++}}END{for(i in count) {print i,count[i]}}'/etc/fstab # 遍历文件统计单词出现次数
十一、函数
[root@localhost ~]#netstat -tan | awk'/^tcp\>/{split($5,ip,":");count[ip[1]]++}END{for (i in count){print i,count[i]}}'
本文出自 “许鼎的博客” 博客,请务必保留此出处http://xuding.blog.51cto.com/4890434/1770983