awk-数组

数组名[数组下标]=值

root@ls4E7syj1iCHuan:/etc/apt# awk 'BEGIN{students["1"]="aa";students["2"]="bb";students["3"]="cc";for(k in students){print k,students[k];}}'
3 cc                                                                                                                      数组下标    值
2 bb
1 aa

 

[root@localhost ~]# awk 'BEGIN{tB["a"]="LISTEN";tB["b"]="";tB["c"]="c1";delete tB["a"];for(k in tB){print k,tB[k];}}'
b b1                                                      
c c1

 统计当前主机的TCP协议网络各种连接状态出现的次数                                          

root@ls4E7syj1iCHuan:/etc/apt# netstat -atn|awk '/^tcp/{++count[$NF]} END {for(a in count) print a,count[a]}'
TIME_WAIT 2                                            数组名      取出数组下标
LISTEN 5
ESTABLISHED 5
root@ls4E7syj1iCHuan:~# ss -ant |awk '!/State/{print $1}' |sort  |uniq -c 
      5 ESTAB
      4 LISTEN
      2 TIME-WAIT

 

[root@localhost ~]# awk '{ip[$1]++} END{for(i in ip){print ip[i],i}} ' access_log  | sort -nr | head -n3
4870 172.20.116.228
3429 172.20.116.208
2834 172.20.0.222

 

for ( var in array) { for-body}  遍历数组中每个元素

root@ls4E7syj1iCHuan:/etc/apt# netstat -atn|awk '/^tcp/{++count[$NF]} END {for(a in count) {print a,count[a]}}'
TIME_WAIT 2
FIN_WAIT1 1
LISTEN 4
ESTABLISHED 6

 

root@ls4E7syj1iCHuan:/etc/apt# netstat -atn|awk '/^tcp/{++cou[$NF]} END {for(a in cou) {print a,cou[a]}}'
TIME_WAIT 2
LISTEN 4
ESTABLISHED 7

 

root@ls4E7syj1iCHuan:~# netstat -atn|awk '/^tcp/{count[$NF]++} END {for(a in count) print a,count[a]}'
TIME_WAIT 2
LISTEN 4
ESTABLISHED 5

 

root@ls4E7syj1iCHuan:~# ss -atn|awk '!/State/{++count[$1]} END {for(a in count) print a,count[a]}'
ESTAB 7
LISTEN 4
TIME-WAIT 2

 

root@ls4E7syj1iCHuan:~# ss -ant | awk 'NR!=1 {count[$1]++}END{for (i in count) print i,count[i]}'
ESTAB 7
LISTEN 4
TIME-WAIT 2

 

 

[root@localhost ~]# cat a.txt 
a
b
c
d
e
f
g
b
c
b
c
d [root@localhost
~]# awk 'line[$0]{print $0}' a.txt
$0整行作为下标赋值给数组line
line['a']="" 空 [root@localhost
~]# awk '!line[$0]{print $0}' a.txt # !空取反都为1,为真,都为真,遍历都打印 a b c d e f g b c
b
c
d [root@localhost
~]# awk '!line[$0]++{print $0}' a.txt #++,1++为2,2++为3为真,真取反为假0,假0都不打印,达到去重效果 a b c d e f g

 

[root@localhost ~]# awk '!line[$0]++{print $0}' a.txt 
a
b
c
d
e
f
g

 

 

[root@localhost ~]# cat aa.txt 
a
b
c
d
e
f
g
b
c
b
c
d
[root@localhost ~]# awk 'line[$0]++{print $0}' aa.txt
b
c
b
c
d
[root@localhost ~]# awk 'line[$0]++{print line[$0]}' aa.txt
2
2
3
3
2

 

[root@localhost ~]# awk 'line[$0]++{print $0,line[$0]}' aa.txt
b 2
c 2
b 3
c 3
d 2

 阿达

[root@localhost ~]# cat score.txt 
name sex sore
aaa  f  100
bbc  m  90
ccc  m  95
ddd  f  85
[root@localhost ~]# awk 'NR!=1{stu[$2]++}END{for(i in stu){print i,stu[i]}}' score.txt 
m 2
f 2
[root@localhost ~]# awk 'NR!=1{score[$2]+=$3;num[$2]++}END{for(i in num){print i,num[i],score[i]}}' score.txt 
m 2 185
f 2 185
[root@localhost ~]# vi score.txt 
[root@localhost ~]# awk 'NR!=1{score[$2]+=$3;num[$2]++}END{for(i in num){print i,num[i],score[i]/num[i]}}' score.txt 
m 2 92.5
f 2 93

 

posted @ 2022-04-26 15:17  gg888666  阅读(72)  评论(0编辑  收藏  举报