This may help you with finding an exact string before and after matching strings in a command.
You'll have to use two grep commands, using the pipe "|" operator. The command in the first pipe uses grep to print out all the text that appears a specified number of lines before the matching string, and an additional pipe operator makes grep to output the exact text after the matching string, ignoring the exact text that appears before or after the targeted string, further narrowing down the output.
Here's the template combination command that outputs only a specific string that's located specified number of lines before the specified string in the first "grep -B" pipe (replace the text between the {} symbols with your own):
{command-with-desired-output} | grep -B {number-of-lines-between-text-after-and-wanted-text} '{matching-string-after-wanted-value}' | grep -oP '(?<={exact-string-before-wanted-value}).*')
The "grep -B {number}" parameter prints all the contents for a desired number of lines that appear before the matching string, including the line matched, while cutting off everything that appears after the desired number of lines is reached - use "grep -A" if the desired value appears after the matching string.
The second grep command. "grep -oP", finds the matching string and prints everything after it until it reaches the end of the current line, while excluding the matching string.
Example command I used to find the Sink Input of a gmediarender sink:
pactl list sink-inputs | grep -B 20 'application.name = "gmediarender"' | grep -oP '(?<=Sink Input #).*')
An easy way to find how many lines before the matched string there are is to paste the output of just the grep -B command into gedit (you can use CTRL+SHIFT+C to copy highlighted text from the terminal, or right-click and copy), and read the amount of lines there - it will appear in the lower right corner of gedit's window as "Ln".
sudo apt-get install -y gedit