1、print相關實例
linux
[root@localhost ~]# tail -4 /etc/fstab | awk '{print $2,$4}'數組
[root@localhost ~]# tail -4 /etc/fstab | awk '{print "hello:"$1}'bash
[root@localhost ~]# tail -4 /etc/fstab | awk '{print "hello:" $1}'tcp
[root@localhost ~]# tail -4 /etc/fstab | awk '{print "hello:$1"}'ide
[root@localhost ~]# tail -4 /etc/fstab | awk '{print "hello:"$1 1234567}'函數
[root@localhost ~]# tail -4 /etc/fstab | awk '{print "hello:"$1,1234567}'ui
2、變量使用實例spa
-F:等於-v FS=':'3d
[root@localhost ~]# tail -3 /etc/passwd | awk -F: '{print $1 }'orm
[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}'
3、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}'
4、條件判斷操做符實例
[root@localhost ~]# tail -5 /etc/passwd | awk -F: '{$3>=1000?usertype="Common User":usertype="Sysadmin or SysUser";printf "%-15s:%-s\n",$1,usertype}'
5、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"}'
6、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 "Common user:%-34sUid:%d\n",$1,$3} else {printf "Root or Sysuser:%-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}'
7、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)};
8、for循環
[root@localhost ~]# awk '/^[[:space:]]*linux16/{for(i=1;i<=NF;i++) {print $i,length($i)}}' /etc/grub2.cfg
9、next
[root@localhost ~]# tail -10 /etc/passwd | awk -F: '{if($3%2!=0) next; print $1,$3}'
10、數組
[root@localhost ~]# netstat -tan | awk '/^tcp\>/{state[$NF]++}END{for(i in state) { print i,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 # 遍歷文件統計單詞出現次數
11、函數
[root@localhost ~]# netstat -tan | awk '/^tcp\>/{split($5,ip,":");count[ip[1]]++}END{for (i in count) {print i,count[i]}}'