title: sed with g not replacing all occurrences in a file when using a variable as a regex match
body: <p>I am having some trouble with sed replacements.</p>
  <p>I am working with csv files that are often concatenated together that contain a serial number above the headers and data that I need that I actually need. The serial number will always be the same for all the data in the file. The serial number does not contain any white space and is purely numbers in content. The first occurrence of the serial number will always be on the first line of the file.</p>
  <p>Example:</p>
  <pre><code>123456789
  
  Header1,Header2,Header3
  
  Data1,Data2,Data3
  
  
  123456789
  
  Header1,Header2,Header3
  
  Data1,Data2,Data3
  </code></pre>
  <p>As part of a larger script I need to remove all the serial numbers, which wouldn't normally be an issue but I am processing csv files from multiple devices and all of them have unique serial numbers, so each individual csv file will contain a different serial number.</p>
  <p>What I have tried so far is:</p>
  <p><em>Attempt 1:</em></p>
  <pre><code>foo=$(sed -n '1p' file.csv)
  sed -i &quot;s/$foo//g&quot; file.csv
  </code></pre>
  <p>This only replaces the first occurrence in the file and leaves the other occurrences unaltered, which I really don't understand because when I manually run sed with the serial number instead of the variable it works fine and replaces all the occurrences. At first I thought there might be a chance that the script was re-evaluating the $foo after the first occurrence but after adding a couple of &quot;echo $foo&quot;'s in that does not seem to be the case.</p>
  <p>The output looks like this:</p>
  <pre><code>Header1,Header2,Header3,
  
  Data1,Data2,Data3
  
  
  123456789
  
  Header1,Header2,Header3
  
  Data1,Data2,Data3
  </code></pre>
  <p>I really hope that I just have some tunnel vision and am missing something really basic.</p>
  <p>Thank you in advanced for any assistance.</p>