Processing text is awk's strength. No matter how fast the performance is, it is amazing. here is a brief introduction.
Copy codeThe code is as follows:
[Chengmo @ localhost ~] $ Awk 'In in {
While ("netstat-an" | getline ){
If ($5 ~ /[1-255]/)
{
Split ($5, t1 ,":");
Tarr [t1 [1] ++;
}
}
For (k in tarr)
{
Print k, tarr [k] | "sort-r-n-k2 ";
}
};'
$5 is the 5th netstat-an Field. By default, the ip address and port of the peer connection are used.
[Chengmo @ localhost ~] $ Time awk 'In in {while ("netstat-an" | getline) {if ($5 ~ /[1-255]/) {split ($5, t1, ":"); tarr [t1 [1] ++ ;}} for (k in tarr) {print k, tarr [k] | "sort-r-n-k2 ";}};'
211.151.33.14 28
113.65.21.200 14
121.32.89.106 13
60.191.178.230 12
118.133.177.104 12
58.61.152.154 11
219.137.58.20 11
124.117.248.52 11
122.198.80.145 11
222.88.15.138 10
222.44.13.191 10
222.161.47.34 10
221.4.202.210 10
218.79.64.12 10
183.17.92.193 10
120.72.128.254 10
118.118.121.244 10
116.252.38.236 10
113.248.75.44 10
Real 0m1. 149 s
User 0m0. 032 s
Sys 0m1. 055 s
Awk common application series, always updated! I will summarize some of my server management code over the years and learn from my friends.