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]#

Tuesday, January 26, 2021

Unix Scripting : extract value at Nth line after string

 To extract the Nth line after a matching pattern you want:

awk 'c&&!--c;/pattern/{c=N}' file

sample use in script
#! /bin/sh
# check pid for OrderManagement 
PID=`ps -ef | grep Order | grep OrderManagement| awk '{print $2}'`
if [ -z "$PID" ]
then
# return 0 if PID does not exist
echo 0
else
# If PID exist ,test if jmap okay
if jmap -heap $PID > /dev/null 2>&1
then
echo pid is $PID
#run jmap -heap PID
/usr/bin/jmap -heap $PID | awk 'c&&!--c;/G1 Heap/{c=5}' | awk -F% '{print $1}' | cut -d. -f1 | sed 's/ //g'
else 
# return 0 if jmap command fails
echo 0
fi
fi


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...