AWK中变量无需声明即可直接使用,NF记录着当前行的列数,即单词数量,所以将其相加即可:
awk '{ x+=NF } END { print x}' ColorBlind.txt
awk '{ if ( !line[$0] ) print $0; line[$0]++ }' ColorBlind.txt
awk 'length > 20 ' ColorBlind.txt
awk '/black & white/ {x++} END{print x}' ColorBlind.txt
awk '{gsub(/[0-9]+/,"");print}' /tmp/passwd
awk '/Failed/{print $(NF-3)}' secure-20161219 |sort |uniq -c |sort -nk1
awk '/Failed/{fa[$(NF-3)]++}END{for(pol in fa)print pol,fa[pol]}' secure-20161219 |sort -rnk2
awk '/Failed/{print $(NF-5),$(NF-3)}' secure-20161219 >user-ip.log
awk '{h[$1" "$2]++}END{for(p in h) print p,h[p]}' user-ip.log |head
awk '/Failed/{h[$(NF-5)" "$(NF-3)]++}END{for(p in h) print p,h[p]}' secure-20161219 |head
awk '{h[$1]++}END{for(i in h) print i,h[i]}' access.log |sort -nrk2 |head
[root@clsn6 awk]# awk '{sum=sum+$10}END{print sum}' access.log
2478496663
[root@clsn6 awk]# awk '{sum=sum+$10}END{print sum/1024^3}' access.log
2.30828
[root@clsn6 awk]# awk '{h[$1]=h[$1]+$10}END{for(p in h) print p,h[p]/1024^2 }' access.log |sort -rnk2|column -t |head
114.83.184.139 29.91
117.136.66.10 21.3922
116.216.30.47 20.4716
223.104.5.197 20.4705
116.216.0.60 18.2584
[root@clsn6 awk]# awk '{count[$1]++;sum[$1]=sum[$1]+$10}END{for(pol in sum)print pol,count[pol],sum[pol]}' access.log |sort -nrk2 |column -t |head
58.220.223.62 12049 12603075
112.64.171.98 10856 15255013
114.83.184.139 1982 31362956
117.136.66.10 1662 22431302
115.29.245.13 1318 1161468
223.104.5.197 961 21464856
116.216.0.60 957 19145329