title: Powershell: Replace last occurence of a line in a file
body: <p>Suppose we have a file foo.txt:</p>
  
  <pre><code>abc
  abc
  abc
  </code></pre>
  
  <p>And I want to turn it into:</p>
  
  <pre><code>abc
  abc
  HelloWorld
  </code></pre>
  
  <p>This command should work:</p>
  
  <pre><code>(gc foo.txt -Raw) -replace '(.*)abc', '$1HelloWorld' | Out-File foo.txt
  </code></pre>
  
  <p>Because I'm using -Raw switch, which reads the entire file instead of one line at a time. Then, the greedy pattern (.*) should match everything up to the last "abc" and replace it with "HelloWorld".</p>
  
  <p>But it doesn't work. All 3 occurences of "abc" are replaced with "HelloWorld", as if "-replace" was processing the file one line at a time, despite the -Raw switch.</p>
  
  <p>Why?</p>