In several different linux machines I have to count the number of tcp socket connections and their status for different ports. At the end the printout might look like this.
49570 10.10.10.10:13062 ESTABLISHED
783 10.10.10.10:18080 CLOSE_WAIT
493 10.10.10.10:18082 CLOSE_WAIT
109 10.10.10.10:18080 SYN_RECV
17 10.10.10.10:15062 TIME_WAIT
15 10.10.10.10:15062 ESTABLISHED
The first column is the count, second ip:port, third the status.
What I want to do is to reformat the output so that it comes out like this
13062 15062 18080 18082
ESTABLISHED 49570 15 0 0
CLOSE_WAIT 0 0 783 493
SYN_RECV 0 0 109 0
TIME_WAIT 0 17 0 0
The ip, is different from machine to machine, there could be more ports as well, or more statuses, or less. Is it possible to achieve this with awk. Does anyone have an example on how to get this.
Sorry I was having a hard time to paste the input/output result desired, it is as above now. Thanks so much in advance.
$ cat tst.awk
{
sub(/.*:/,"",$2)
ports[$2]
statuses[$3]
counts[$2,$3] = $1
for (i=1;i<=NF;i++) {
maxWidth[i] = (length($i) > maxWidth[i] ? length($i) : maxWidth[i])
}
}
END {
statusWidth = maxWidth[3]
otherWidth = (maxWidth[1] > maxWidth[2] ? maxWidth[1] : maxWidth[2]) + 2
printf "%-*s", statusWidth, ""
for (port in ports) {
printf "%*s", otherWidth, port
}
print ""
for (status in statuses) {
printf "%-*s", statusWidth, status
for (port in ports) {
printf "%*d", otherWidth, counts[port,status]
}
print ""
}
}
$ awk -f tst.awk file
13062 15062 18080 18082
SYN_RECV 0 0 109 0
CLOSE_WAIT 0 0 783 493
ESTABLISHED 49570 15 0 0
TIME_WAIT 0 17 0 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With