Search code examples
bashshellechonetstat

bash shell: echo 3 list of output netstat to 3 column


Hi every one i have a script shell like this:

#!/bin/bash
netstatport80=`netstat -an|grep :80 |awk '!/:8080/'|awk '!/:8081/' |awk '{print $5}'|cut -d":" -f1|sort|uniq -c|sort -rn| grep -v "0.0.0.0"`

netstatport443=`netstat -an|grep :443 |awk '!/:8080/'|awk '{print $5}'|cut -d":" -f1|sort|uniq -c|sort -rn| grep -v "0.0.0.0"`

netstatESTA=`netstat -an|grep ESTA |awk '!/:8080/'|awk '{print $5}'|cut -d":" -f1|sort|uniq -c|sort -rn| grep -v "0.0.0.0"`

echo '
List IP request port 80:    List IP request port 443:    List IP request ESTA:
'$netstatport80'            '$netstatport443'           '$netstatESTA'

'

How i can have a output like that:

List IP request port 80:    List IP request port 443:    List IP request ESTA:
123.x.x.x                    183.x.x.x                   153.x.x.x
193.x.x.x                    123.x.x.x                   164.x.x.x
130.x.x.x                    103.x.x.x                   101.x.x.x
187.x.x.x                    173.x.x.x                   185.x.x.x

Thanks all your help!


Solution

  • paste is the standard tool for your need, it will join multiple files into columns :

    $ echo """A
    > a
    > a
    > a""" > A.txt
    
    $ echo """B
    > b
    > b""" > B.txt
    
    $ echo """C
    > c
    > c
    > c
    > c
    > c""" > C.txt
    
    $ paste A.txt B.txt C.txt
    AAAA    BB      C
    a       b       c
    a       b       c
    a               c
    a               c
    a               c
                    c
                    c
    

    It works on files so you should write the output of your filtered netstat commands to files, or you can use anonymous pipes :

    paste <(echo "List IP request port 80:"; echo "$netstatport80") <(echo "List IP request port 443:"; echo "$netstatport443") <(echo "List IP request ESTA:"; echo "$netstatESTA")
    

    If the sizes of the different columns break the formatting, you can use paste's -d option to specify a delimiter between columns, then reformat them using the column tool, or try with pr which will truncate columns at a specific size (see the previous version of my answer below).


    Edit : older answer using pr, which is overkill and less known.

    Looks like you could be using pr 'merge' -m option :

    $ echo """A
    > a
    > a
    > a""" > A.txt
    
    $ echo """B
    > b
    > b""" > B.txt
    
    $ echo """C
    > c
    > c
    > c
    > c
    > c""" > C.txt
    
    $ pr -mT A.txt B.txt C.txt
    A                       B                       C
    a                       b                       c
    a                       b                       c
    a                                               c
                                                    c
                                                    c
    

    pr is a pager I found out about in this SO answer while researching your question, its -m flag answers your need and it's -T option disables page formatting (headers, footers, and maybe more?).
    It takes input from files so you will have to redirect the output of your filtered netstat commands to files instead of using variables, unless you want to bother with anonymous pipes :

    pr -mT <(echo "List IP request port 80:"; echo "$netstatport80") <(echo "List IP request port 443:"; echo "$netstatport443") <(echo "List IP request ESTA:"; echo "$netstatESTA")