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


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