awk模式匹配語法
/正則表達式/ {匹配後的操作}
#cat shell_recorder.awk
#! /bin/bash # shell_recorder.awk BEGIN { print "SHELL USAGE:" } /bash/{++bash} /nologin/{++nologin} END { print "We have "bash" bash users." print "We have "nologin" nologin users." }
#awk -f shell_recorder.awk /etc/passwd
We have 4 bash users.
We have 29 nologin users.
--------------------------------------------------------------------------
也可以使用以下的方法得到同樣的結果:
#cat recorder.sh
#! /bin/bash # recorder.sh bash=`cat /etc/passwd | grep 'bash' | wc | awk '{print $1}'` nologin=`cat /etc/passwd | grep 'nologin' | wc | awk '{print $1}'` echo "We have $bash bash users." echo "We have $nologin nologin users."
# sh recorder.sh
We have 4 bash users.
We have 29 nologin users.