title: Bash: Find a string before another string in a file
body: <p>I am trying to find the preceding string before another string. For example:</p>
  
  <pre><code>StringA &lt;stuff 1&gt;
  StringA &lt;stuff 2&gt;
  StringD
  ...
  StringB &lt;stuff 3&gt;
  StringA &lt;stuff 4&gt;
  StringA &lt;stuff 5&gt;
  StringA &lt;stuff 6&gt;
  StringD
  StringB &lt;stuff 7&gt;
  </code></pre>
  
  <p>I want to find all the "StringA" that JUST preceeds StringB in the file.</p>
  
  <p>The output in this example would be:</p>
  
  <pre><code>StringA &lt;stuff 2&gt;
  StringA &lt;stuff 6&gt;
  </code></pre>
  
  <p>I am able to do find the line numbers of all the StringB by using: grep -n "StringB"</p>
  
  <p>Then I can use sed -n 1,$line_numberp which makes me go from line 1 to the line of StringB. I do grep "StringA" | tail -n1</p>
  
  <p>This seems to be work but is a bit cumbersome. Is there any better way to achieve the desired result please?</p>