title: Replacing first and second occurrence of the same text with different values
body: <p>I'm searching for a way to replace the first occurrence of a certain text in a text file with a value ${A} and the second occurrence of the same text, on a different line, with ${B}. Can this be achieved with sed or awk or any other UNIX tool?</p>
  
  <p>The toolset is fairly limited: bash, common UNIX tools like sed, grep, awk etc. Perl, Python, Ruby etc. cannot be used...</p>
  
  <p>Thanks in advance for any advice
  Robert</p>
  
  <p>Example:</p>
  
  <pre><code>...
  Text
  Text
  Text
  Text
  TEXT_TO_BE_REPLACED
  Text
  Text
  Text
  TEXT_TO_BE_REPLACED
  Text
  Text
  Text
  ...
  </code></pre>
  
  <p>should be replaced with</p>
  
  <pre><code>...
  Text
  Text
  Text
  Text
  REPLACEMENT_TEXT_A
  Text
  Text
  Text
  REPLACEMENT_TEXT_B
  Text
  Text
  Text
  ...
  </code></pre>