INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

script output to columns with headers

script output to columns with headers

(OP)
netstat -an | grep tcp | awk '{print $6}' | sort | uniq -c | sort -nr

CODE

294 FIN_WAIT_2
173 ESTABLISHED
 83 TIME_WAIT
 33 LISTEN
  9 CLOSED
  3 CLOSING
  2 CLOSE_WAIT
  1 SYN_RCVD 

Would like the output to be in the following format instead

CODE

FIN_WAIT_2 ESTABLISHED TIME_WAIT LISTEN CLOSED CLOSING CLOSE_WAIT SYN_RCVD
294        172         83        33     9      3       2          1 

I figured initial write of this file would create headers, then subsequent writes would only add numbers for stats collected a row at a time.

RE: script output to columns with headers


Try something like this:

CODE

netstat -an | grep tcp |awk '{x[$6]++;} END {asorti(x);
 for(i in x){printf "%s ",i};print ""
 for(i in x){printf "%s ",x[i]};print ""
}' 
3eyes

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

RE: script output to columns with headers

(OP)
This is on AIX 6 using KSH. I changed awk to gawk and getting the columns and some numbers.

4 5 6 7 1 2 3
FIN_WAIT_2 LAST_ACK LISTEN TIME_WAIT CLOSED CLOSE_WAIT ESTABLISHED

RE: script output to columns with headers

printf is your solution.

CODE --> bash

netstat -an | grep tcp |awk '{x[$6]++;} END {
 for(i in x){printf "%s ",i};print "";
 for(i in x){printf "%s ",x[i];print ""}
}' |awk '{printf "%15s%15s%15s%15s%15s%15s\n",$1,$2,$3,$4,$5,$6}' 
15 is the size you want to apply to each field.

Good luck.

RE: script output to columns with headers

(OP)
Here is a more fully developed script to account for states that had 0 connections. Data was being shifted around otherwise. Still not sure how to get the format I'm looking for in one go without scrubbing the data.

CODE

dt=`date +"%m%d%y%H%M%S"`
netstat -an | grep tcp | awk '{print $6}' | sort | uniq -c | sort -nr > /tmp/tcpconn.out
for state in SYN_SENT SYN_RECEIVED TIME_WAIT LISTEN FIN_WAIT_2 ESTABLISHED CLOSE_WAIT CLOSED LAST_ACK FIN_WAIT_1 CLOSING
do
k=`grep $state /tmp/tcpconn.log`
if [ "$k" == "" ]
then
echo "0 $state" >> /tmp/tcpconn.log
fi
done
while read line
do
echo "$dt $line" >> /tmp/logs/tcpconn.out
done < /tmp/tcpconn.log 

which gives us

CODE

050313094000 480 FIN_WAIT_2
050313094000 322 ESTABLISHED
050313094000 101 TIME_WAIT
050313094000 33 LISTEN
050313094000 9 CLOSED
050313094000 2 LAST_ACK
050313094000 1 CLOSE_WAIT
050313094000 0 SYN_SENT
050313094000 0 SYN_RECEIVED
050313094000 0 FIN_WAIT_1
050313094000 0 CLOSING 

But this is still what I'm trying to achieve

CODE

TIME        FIN_WAIT_2    ESTABLISHED    TIME_WAIT    LISTEN    CLOSED    LAST_ACK    CLOSE_WAIT    SYN_SENT    SYN_RCVD    FIN_WAIT_1    CLOSING
093500        294            172            83            33        19        3            2            1            0            0            0
094000        194             63            33            93        41        8            1            3            0            0            0
094500        341            112            45            13        7        11            0            7            0            0            0 
095000        498            252            71            49        12        2            5            1            0            0            0 

RE: script output to columns with headers

I suggest:

CODE --> bash

$ cat init.txt 
050313094000 480 FIN_WAIT_2 
050313094000 322 ESTABLISHED 
050313094000 101 TIME_WAIT 
050313094000 33 LISTEN 
050313094000 9 CLOSED 
050313094000 2 LAST_ACK 
050313094000 1 CLOSE_WAIT 
050313094000 0 SYN_SENT 
050313094000 0 SYN_RECEIVED 
050313094000 0 FIN_WAIT_1 
050313094000 0 CLOSING
$ cat horizontalToVertical.awk 
#!/bin/bash

awk '(NR==1){
        time=substr($1,7,6)
} 

{
        for (i=2;i<=NF;i++) 
                a[i]=a[i]" "$i
} 

END{
        for (i in a) 
                if (i==3) #header
                        print "TIME",a[i]
                else
                        print time,a[i]
} ' init.txt |awk '{
        for (i=1;i<=NF;i++) 
                printf "%13s",$i
        print ""
}'
$ ./horizontalToVertical.awk init.txt >result.txt 
$ cat result.txt 
       094000          480          322          101           33            9            2            1            0            0            0            0
         TIME   FIN_WAIT_2  ESTABLISHED    TIME_WAIT       LISTEN       CLOSED     LAST_ACK   CLOSE_WAIT     SYN_SENT SYN_RECEIVED   FIN_WAIT_1      CLOSING
$ 
If you want to stack many logs, I suggest you erase the printing of headers in the script, and get ready a file with headers in it. So, you can send all lines into it and have a good file.

Other idea: split the script above into 2 scripts: one to have a re-usable csv file, and one to make a beautifull display. If you do so, the first line will become "#!/usr/bin/awk" and you will be able to keep only awk commands.

I hope this is clear.

RE: script output to columns with headers



Your "Trying to achieve" results do not match your posted data.
Here is a starter:

CODE

TMPDIR=/tmp
LOGDIR=/tmp/log
dt=`date +"%m%d%y%H%M%S"`
netstat -an | grep tcp |awk 'BEGIN {split("CLOSE_WAIT,CLOSED,CLOSING,ESTABLISHED,FIN_WAIT_1,FIN_WAIT_2,LAST_ACK,LISTEN,SYN_RECEIVED,SYN_SENT,TIME_WAIT",tx,","); for(x in tx){t[tx[x]]=0}}{t[$6]++} END {for(x in t) print t[x],x}'|sort -nr| xargs -i+ echo $dt + 
Which produces:

CODE

==> ./m2
050313143251 68 ESTABLISHED
050313143251 39 LISTEN
050313143251 2 CLOSE_WAIT
050313143251 2 CLOSED
050313143251 0 TIME_WAIT
050313143251 0 SYN_SENT
050313143251 0 SYN_RECEIVED
050313143251 0 LAST_ACK
050313143251 0 FIN_WAIT_2
050313143251 0 FIN_WAIT_1
050313143251 0 CLOSING 
thumbsup2

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

RE: script output to columns with headers



Or better this:

CODE

TMPDIR=/tmp
LOGDIR=/tmp/log
dt=`date +"%m%d%y%H%M%S"`
netstat -an | grep tcp |awk 'BEGIN {split("CLOSE_WAIT,CLOSED,CLOSING,ESTABLISHED,FIN_WAIT_1,FIN_WAIT_2,LAST_ACK,LISTEN,SYN_RECEIVED,SYN_SENT,TIME_WAIT",tx,",");for(x in tx){t[tx[x]]=0}}{t[$6]++} END {
for(x in t){printf "%12s ",x};print ""; 
for(x in t){printf "%12s ",t[x]};print ""}' 

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

RE: script output to columns with headers

(OP)
LKBrwnDBA, thanks a bunch. For some reason, my data was not parsing right with the other awk's, but yours ( both of them ) sorted the data quite well. Thank you.

RE: script output to columns with headers

(OP)
Now my brain is thinking...in a single script, could I run the initial bit to create the headers one time, then only print the numbers after? Something silly like the following

CODE

while true
do
clear
date
echo
netstat -an | grep tcp |awk 'BEGIN {split("CLOSE_WAIT,CLOSED,CLOSING,ESTABLISHED,FIN_WAIT_1,FIN_WAIT_2,LAST_ACK,LISTEN,SYN_RECEIVED,SYN_SENT,TIME_WAIT",tx,",");for(x in tx){t[tx[x]]=0}}{t[$6]++} END
{
for(x in t){printf "%12s ",x};print "";
for(x in t){printf "%12s ",t[x]};print ""}'
sleep 60
done 

And we could watch connections come and go 'live' but also, would be easy to just push this to a file with something like tee and have an Excel ready data file as well.

RE: script output to columns with headers


Try this:

CODE

dt=`date +"%m%d%y%H%M%S"`
echo "Date: $dt"
echo ""|awk 'BEGIN{split("CLOSE_WAIT,CLOSED,CLOSING,ESTABLISHED,FIN_WAIT_1,FIN_WAIT_2,LAST_ACK,LISTEN,SYN_RECEIVED,SYN_SENT,TIME_WAIT",tx,",");for(x in tx){t[tx[x]]=0}} END {
for(x in t){printf "%12s ",x};print "";
}'
while true
do
  netstat -an | grep tcp |awk 'BEGIN {split("CLOSE_WAIT,CLOSED,CLOSING,ESTABLISHED,FIN_WAIT_1,FIN_WAIT_2,LAST_ACK,LISTEN,SYN_RECEIVED,SYN_SENT,TIME_WAIT",tx,",");for(x in tx){t[tx[x]]=0}}{t[$6]++} END {
  #for(x in t){printf "%12s ",x};print "";
  for(x in t){printf "%12s ",t[x]};print "";
  }'
  sleep 30
done 
thumbsup2

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

RE: script output to columns with headers

(OP)
:D

Giddyup! That works perfectly. Amazing work LKBrwnDBA, thanks a ton.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close