Thursday, February 18, 2021

UNIX: How to print column nicely using printf

[user@hostfwnms1-oam tmp]# cat b.sh

printf "%-26s %-19s %-8s %-8s %-s %-s\n" HOSTNAME IP PING SNMPWALK 0-ok 1-fail

for i in `cat newhost2021 | awk '{print $1}'`

do

HOSTNAME=`cat newhost2021 | grep $i | awk '{print $2}'`

HOST=$i

ping -c 3 -W 2 $i > /dev/null

PINGSTATUS=$?

snmpwalk -v 2c -c public $i &> /dev/null

SNMPWLKSTATUS=$?

#echo "$HOSTNAME $HOST $PINGSTATUS $SNMPWLKSTATUS"| awk '{ print $1"\t\t" $2"\t\t" $3"\t\t" $4}'

echo "$HOSTNAME $HOST $PINGSTATUS $SNMPWLKSTATUS"| awk '{ printf "%-26s %-19s %-8s %-8s\n", $1, $2, $3, $4}'

done


[user@hostfwnms1-oam tmp]# sh b.sh

HOSTNAME                   IP                  PING     SNMPWALK 0-ok 1-fail

hostfwhostlab2-ilo          101.123.125.18       0        1       

hostfwsanctr05ctr1          101.123.125.22       0        1       

hostfwsanctr05ctr2          101.123.125.23       1        1       

hostcdrhost1-ilo            101.123.125.24       0        1       

hostcdrhost1-oam            101.123.126.28       0        1       

hostcdrapp1-oam             101.123.126.38       0        0       

hostfwmgmtsw                101.123.125.27       0        0       

[user@hostfwnms1-oam tmp]#

No comments:

UNIX: How to print column nicely using printf

[user@hostfwnms1-oam tmp]# cat b.sh printf "%-26s %-19s %-8s %-8s %-s %-s\n" HOSTNAME IP PING SNMPWALK 0-ok 1-fail for i in `cat n...