netstat | awk '/server/ {for(i=1;i<2;i++) {getline;print}'
Output:
ESTABLISHED
ESTABLISHED
ESTABLISHED
ESTABLISHED
ESTABLISHED
TIME_WAIT
TIME_WAIT
From the above command I'm getting all the states. I want to count the states and write to a file, like
"Count of ESTABLISHED is: 5"
"Count of TIME_WAIT is : 2"
How to count in awk, other than passing the output to grep and using -c option.
Looks like you managed to print just the state of each server.
Now, try to push each state as the key in an associative array and keep incrementing the value each time you find the same state in the array keys.
from that I used ur suggestion which produced the below o/p:
netstat -a | grep server1 | awk '/LISTEN|ESTABLISHED|TIME_WAIT/{a[$6]++}; END { for(b in a) print "Count of "b" is: "a}'
Count of LISTEN is:2
Count of ESTABLISHED is: 18
Count of TIME_WAIT is: 5