I need to find two matches in the output from ps. I am searching with ps -ef |grep mysql for:

/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]"

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.

closed as unclear what you're asking by Jeff Schaller, Mr Shunz, Christopher, RalfFriedl, Archemar Jan 29 at 9:28

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 6
    Perhaps there's a less-fragile way to find that process? Is it an ancestor or child of the MySQL process? – Jeff Schaller Jan 25 at 18:28
  • Any ps -ef output processing which requires the processes to be listed in a particular order relative to the command lines of the processes will fail at least some of the time. ps -ef output is ordered by the PID, and those wrap around. It's also possible for other processes being started asynchronously from the ones you are interested in to just happen to start in between the processes you want to find. – Ed Grimm Jan 26 at 2:28
  • Your grep command line is clearly not complete, as it is at least missing a closing quote. I am also unable to produce ps -ef output that has any lines that start with ok:. – Ed Grimm Jan 26 at 3:11
  • I should have mentioned that I am utilizing grep on the results of an ansible run (JSON) . I used ps -ef in a script that is running via ansible on remote servers. The returned data is JSON and that is what I am parsing. – Sloan Jan 28 at 18:45