Originally posted by: SystemAdmin
I am having a strange issue. I have an awk script that analyzes netstat output. When I run the script it formats it perfectly to the screen. However when I > it into a file some of the lines are reorganized.
The following is the section of code that does the printing. I have simplified the script to make it easier to see and prevent from having to publish IP addresses in the full output. I have also attached the full script as an attachment if that would help, however even the shortened script has the issue:
{ print
"Local port totals"; printf
"%8s %8s %10s %10s\n",
"Port",
"Count",
"Recv-Q",
"Send-Q"; sort =
"sort -rn";
for (i in local_count)
{
if (local_count[i] > 1)
{ split(i,g,
" "); printf
"%8s %8s %10s %10s\n", g[1], local_count[i], local_recvq[i], local_sendq[i] | sort;
}
} close(sort); print
"\nState totals"; printf
"%16s %8s %10s %10s\n",
"State",
"Count",
"Recv-Q",
"Send-Q"; sort =
"sort";
for (i in state_count)
{
if (state_count[i] > 1)
{ split(i,g,
" "); printf
"%16s %8s %10s %10s\n", g[3], state_count[i], state_recvq[i], state_sendq[i] | sort;
}
} close(sort); print
"\nLocal port by state"; printf
"%8s %16s %8s %10s %10s\n",
"Port",
"State",
"Count",
"Recv-Q",
"Send-Q"; sort =
"sort -rn";
for (i in local_state_count)
{
if (local_state_count[i] > 1)
{ split(i,g,
" "); printf
"%8s %16s %8s %10s %10s\n", g[1], g[3], local_state_count[i], local_state_recvq[i], local_state_sendq[i] | sort;
}
} close(sort);
}
Here is the output on the screen when i run netstat -an | netstat_analyze.awk
Local port totals Port Count Recv-Q Send-Q 50000 99 0 0 47001 2 0 0 22 4 0 52 State totals State Count Recv-Q Send-Q LISTEN 32 0 0 ESTABLISHED 108 0 52 Local port by state Port State Count Recv-Q Send-Q 50000 ESTABLISHED 99 0 0 47001 ESTABLISHED 2 0 0 22 ESTABLISHED 4 0 52
Here is the output in the netstat_report file when i run netstat -an | netstat_analyze.awk > netstat_report:
50000 99 0 0 47001 2 0 0 22 4 0 52 LISTEN 32 0 0 ESTABLISHED 108 0 52 50000 ESTABLISHED 99 0 0 47001 ESTABLISHED 2 0 0 22 ESTABLISHED 4 0 52 Local port totals Port Count Recv-Q Send-Q State totals State Count Recv-Q Send-Q Local port by state Port State Count Recv-Q Send-Q
If I change the code and pipe and close each individual header line through sort it outputs to a file correctly but that is a hack and extremely inefficient. I have tried embedding the netstat -an and the awk command into the same script and I see the same thing. As a sanity check of the script I tried it on a linux machine and it works just fine, although obviously that doesn't solve anything.
I thought > just redirected the output and didn't do anything that would change it but I am guessing that is not the case.
Any help would be appreciated.
#AIX-Forum