title: Bash: how to use sed to replace only the last occurence in a file?
body: <p>Having a file containing repeated commented lines like:</p>
  
  <pre><code># ScriptAlias /cgi-bin/ "somepath"
  # ScriptAlias /cgi-bin/ "otherpath"
  </code></pre>
  
  <p>I want to add a line only after the last occurence resulting in</p>
  
  <pre><code># ScriptAlias /cgi-bin/ "somepath"
  # ScriptAlias /cgi-bin/ "otherpath"
  ScriptAlias /cgi-bin/ "mypath"
  </code></pre>
  
  <p>To do so I'm using this command:</p>
  
  <pre><code>sed -i 's:^\(.*ScriptAlias /cgi-bin/.*\):\1 \nScriptAlias /cgi-bin/ "mypath":' file
  </code></pre>
  
  <p>But this results in adding my line after each occurence like:</p>
  
  <pre><code># ScriptAlias /cgi-bin/ "somepath"
  ScriptAlias /cgi-bin/ "mypath"
  # ScriptAlias /cgi-bin/ "otherpath"
  ScriptAlias /cgi-bin/ "mypath"
  </code></pre>
  
  <p>How can I tell sed to replace only the last occurence?
  <br>
  <br>
  <strong>EDITED:</strong><br>
  If there's no way to solve it using sed (as said in comments), please provide alternatives reaching the same result, thanks.</p>
  
  <p><br>
  <br>
  <strong>EDITED</strong>:<br>
  The repeated lines can be separeted and with other lines between them like</p>
  
  <pre><code># ScriptAlias /cgi-bin/ "somepath"
  # ScriptAlias /cgi-bin/ "otherpath"
  
  # ScriptAlias /cgi-bin/ "another-path"
  ScriptAlias /foo/ "just-jump"
  # ScriptAlias /cgi-bin/ "that's the last"
  </code></pre>