How to get the process id

How to get the process id from a list of process id got from ps -aef | grep A461. I don't want all the rows. I need only the 2nd row and the pid alone. Then how to kill the process id .

res1=`sqlplus -s << EOF
$AAR_CONNECT
SET SERVEROUTPUT ON SIZE 1000000
SET FEEDBACK OFF
SET LINESIZE 32000
SET HEADING OFF
SET PAGESIZE 0
SET NEWPAGE 0
SET TRIMSPOOL ON
TTITLE OFF
BTITLE OFF
SET VERIFY OFF
SET TERMOUT OFF
SPOOL A460_as_saave_retrieval_master_result1.log
BEGIN
DBMS_OUTPUT.PUT_LINE('NUMBER OF RECORDS BEFORE killing child');
END;
/
SELECT COUNT(*) FROM GFSTU99_ARCH_RETR_ERROR_LOG where msg_id = 38;
SPOOL OFF
EOF
`
The above portion of the script displays the number of records in the table
which is zero.The A460_as_saave_retrieval_master_result1.log gets created.

A460_as_saave_retrieval_master.sh

The above line is a call to the shell. After executing the shell , the ps -aef | grep A461 command list 10 process. I manually kill one process . So
the shell put one entry in the oracle table. But the control will not come out of while loop .The while construct terminates only when all the process created by the shell were killed.

My GOAL is to log an error when one process gets killed.

The below code should get executed after the process gets killed.
So the below script should put an entry in the oracle table.

res2=`sqlplus -s << EOF
$AAR_CONNECT
SET SERVEROUTPUT ON SIZE 1000000
SPOOL A460_as_saave_retrieval_master_result2.log
SET FEEDBACK OFF
SET LINESIZE 32000
SET HEADING OFF
SET PAGESIZE 0
SET NEWPAGE 0
SET TRIMSPOOL ON
TTITLE OFF
BTITLE OFF
SET VERIFY OFF
SET TERMOUT OFF
BEGIN
DBMS_OUTPUT.PUT_LINE('NUMBER OF RECORDS AFTER killing child');
END;
/
SELECT COUNT(*) FROM GFSTU99_ARCH_RETR_ERROR_LOG where msg_id = 38;
SELECT MODULE_CODE,MSG_ID,MSG_LOG_DTS,MSG_LOG_DESC FROM GFSTU99_ARCH_RETR_ERROR_LOG where msg_id = 38;
SPOOL OFF
EOF
`
cat A460_as_saave_retrieval_master_result1.log A460_as_saave_retrieval_master_result2.log > A460_as_saave_retrieval_master_result3.log
rm -f A460_as_saave_retrieval_master_result1.log
rm -f A460_as_saave_retrieval_master_result2.log

If I understood the question correctly, following command can help..

 ps -aef | grep <searchstring> | head -2 | tail -1 | awk '{print $2}'