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