一、print相關執行個體
[root@localhost ~]#
tail -4 /etc/fstab | awk '{print $2,$4}'
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcucTNxE1Qt12ZVlkTCFUQLlHdWpWaUJGdjFDTvl2S39CXCN0LcZ0NvwVMw00LcJDMzZWe39CXt92Yu8GdjFTNuUzcvw1LcpDc0RHaiojIsJye.png)
tail -4 /etc/fstab | awk '{print
"hello:"$1}'
"hello:" $1}'
"hello:$1"}'
"hello:"$1 1234567}'
"hello:"$1,1234567}'
二、變量使用執行個體
-F:等于-v FS=':'
tail -3 /etc/passwd | awk -F: '{print $1 }'
tail -3 /etc/passwd | awk -v FS=':' '{print $1 }'
tail -3 /etc/passwd | awk -v FS=':' '{print $1,$3,$7 }'
tail -3 /etc/passwd | awk -v FS=':' -v OFS='@' '{print $1,$3,$7 }'
head -5 /etc/fstab | awk '{print NF}'
head -5 /etc/fstab | awk '{print $NF}'
tail -4 /etc/fstab | awk '{print NR}'
awk '{print FILENAME}' /etc/fstab /etc/issue
awk 'BEGIN{print ARGC}' /etc/fstab /etc/issue
awk 'BEGIN{print ARGV[0]}' /etc/fstab /etc/issue
awk 'BEGIN{print ARGV[1]}' /etc/fstab /etc/issue
awk 'BEGIN{print ARGV[2]}' /etc/fstab /etc/issue
awk -v test='hello world' 'BEGIN{print test}'
awk 'BEGIN{test="hello world";print test}'
三、printf執行個體
[root@localhost ~]# tail -3 /etc/passwd
| awk -F: '{printf "%s",$1}' # 預設在一行顯示
tail -3 /etc/passwd | awk -F: '{printf "%s\n",$1}'
tail -3 /etc/passwd | awk -F: '{printf "Username:%s\n",$1}'
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}'
'{printf "Username: %-10s, UID:%d\n",$1,$3}'
四、條件判斷操作符執行個體
tail -5 /etc/passwd | awk -F: '{$3>=1000?usertype="Common
User":usertype="Sysadmin or SysUser";printf
"%-15s:%-s\n",$1,usertype}'
五、PATTERN使用執行個體
awk '/^UUID/ {print $1}' /etc/fstab
awk '!/^UUID/ {print $1,$2}' /etc/fstab
awk -F: '$3>=900{printf "%-30s,%d\n",$1,$3}' /etc/passwd
awk -F: '$NF=="/bin/bash"{printf "%-20s%s\n",$1,$NF}'
/etc/passwd
awk -F: '$NF~/bash$/{printf
"%-20s%s\n",$1,$NF}' /etc/passwd
awk -F: '(NR>=10&&NR<=15){print $1}' /etc/passwd
tail -5 /etc/passwd | awk -F: 'BEGIN{print "username uid
\n-----------------------------"}{printf
"%-20s%d\n",$1,$3}'
tail -5 /etc/passwd | awk -F: '{print "username uid
\n-----------------------------";printf
~]# tail -5 /etc/passwd | awk -F:
'BEGIN{print "username
uid
"%-20s%d\n",$1,$3}END{print"===================\n end"}'
六、if-else語句
awk -F: '{if($3>=900) print $1,$3}' /etc/passwd
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}}'
awk -F: '{if($NF=="/bin/bash") print $1}' /etc/passwd
df -h
~]# df -h | awk -F[%] '/^\/dev/{print
$1}' | awk '{if($NF>=20) print $1}'
七、while循環
awk '/^[[:space:]]*linux16/{i=1;while(i<=NF) {print $i,length($i); i++}}'
/etc/grub2.cfg
~]# awk
'/^[[:space:]]*linux16/{i=1;while(i<=NF) {if(length($i)>=7) {print
$i,length($i)};
八、for循環
awk '/^[[:space:]]*linux16/{for(i=1;i<=NF;i++) {print $i,length($i)}}'
九、next
tail -10 /etc/passwd | awk -F: '{if($3%2!=0) next; print $1,$3}'
十、數組
netstat -tan | awk '/^tcp\>/{state[$NF]++}END{for(i in state) { print
i,state[i]}}'
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 # 周遊檔案統計單詞出現次數
十一、函數
netstat -tan | awk
'/^tcp\>/{split($5,ip,":");count[ip[1]]++}END{for (i in count)
{print i,count[i]}}'