[root@nfs-server test]# awk 'BEGIN{array[1]="zhurui";array[2]="zhuzhu";for(key in array) print key,array[key]}'
1 zhurui
2 zhuzhu
[root@nfs-server test]#
[root@nfs-server test]# awk 'BEGIN{array[1]="zhurui";array[2]="zhuzhu";}END {for(key in array) print key,array[key]}'/etc/hosts
1 zhurui
2 zhuzhu
把文件内容第一列作为下标,第二列作为值,放入数组然后输出。
[root@nfs-server test]# cat t3.log
1 zhurui
2 zhuzhu
[root@nfs-server test]# awk 'BENGIN{S[$1]=$2;}END{for(k in S) print k,S[k]}' t3.log
[root@nfs-server test]# awk '{S[$1]=$2;}END{for(k in S) print k,S[k]}' t3.log
1 zhurui
2 zhuzhu
#!/bin/awk
BEGIN{
array[1]="zhurui"
array[2]="zhuzhu"
for(key in array)
print key,array[key];
}
[root@nfs-server test]# awk -f t2.awk
1 zhurui
2 zhuzhu
[root@nfs-server test]#
[root@nfs-server test]# cat t3.log
1 zhurui
2 zhuzhu
[root@nfs-server test]# awk 'BENGIN{S[$1]=$2;}END{for(k in S) print k,S[k]}' t3.log
[root@nfs-server test]# awk '{S[$1]=$2;}END{for(k in S) print k,S[k]}' t3.log
1 zhurui
2 zhuzhu
[root@nfs-server test]# awk -F "/"'{S[$3]=S[$3]+1;}END{for(k in S) print k,S[k]}' test.log
mp3.judong.org 1
post.judong.org 2
www.judong.org 3
[root@nfs-server test]# awk -F "/"'{S[$3]++}END{for(k in S) print k,S[k]}' test.log|sort -rn -k2|head
www.judong.org 3
post.judong.org 2
mp3.judong.org 1
[root@nfs-server test]#
[root@nfs-server test]# awk -F " "'{S[$1]=S[$1]+1;}END{for (k in S) print k,S[k]}' access-test.log|sort -rn|head ##此方法较复杂
10.0.0.3775
10.0.0.1322
10.0.0.1245
10.0.0.1202
10.0.0.1082
10.0.0.1001
[root@nfs-server test]# awk -F " "'{S[$1]++;}END{for (k in S) print k,S[k]}' access-test.log|sort -rn|head ##上一步的简化版
10.0.0.3775
10.0.0.1322
10.0.0.1245
10.0.0.1202
10.0.0.1082
10.0.0.1001
[root@nfs-server test]# awk -F " "'{Z[$1]++;}END{for (k in Z) print k,Z[k]}' access-test.log|sort -rn|head
10.0.0.3775
10.0.0.1322
10.0.0.1245
10.0.0.1202
10.0.0.1082
10.0.0.1001
[root@nfs-server test]# awk '/^tcp/ {S[$NF]++}END{for(k in S) print S[k],k}' netstat.log|sort -rn|head
105 TIME_WAIT
101 ESTABLISHED
55 FIN_WAIT2
7 FIN_WAIT1
5 SYN_RECV
5 LISTEN
3 LAST_ACK
1 CLOSING
[root@nfs-server test]#
[root@nfs-server test]# awk '/^tcp/ {print $NF}' netstat.log|sort|uniq -c|sort -rn|head
105 TIME_WAIT
101 ESTABLISHED
55 FIN_WAIT2
7 FIN_WAIT1
5 SYN_RECV
5 LISTEN
3 LAST_ACK
1 CLOSING
[root@nfs-server test]#