Search file containing ps results for a match "my.cnf" and then for a second match . "ok:" and

I need to find two matches in the output from ps. I am searching with

ps -ef |grep mysql

for:

my.cnf
/bin/sh /usr/bin/mysqld_safe --defaults-file=/data/mysql/master/agis_core/etc/my.cnf

after this match I want to search back and match the hostname which is x number of lines back, above the match my.cnf. the line I want to grab starts with "ok:" and a hostname in brackets "[hostname]"

ok:    [hostname]

I have this grep search but dont really understand it. This grep grabs just the "ok" match but not the entire line, the remainder of this line match contains the hostname I am looking to grab:

grep -Pzo 'my.cnf(?:.*\n)*?\Kok: filename

I want to exclude all lines between, before and after the matches. I want to keep the results of both matches line match one and line match two.

try to remove the option o -Pz
or replace with w -Pzw

1 Like

That works nicely, thanks
I need to search for a second match in the same section "mysql Ver xx.xx" . I really only need to search for

mysql Ver