title: Fastest way to replace string in first row of huge file in linux command line?
body: <p>I have a huge plain text file (~500Gb) on linux machine. I want the replace some string in header line (the first row of the file), but all the method I known seems to be slow and low efficiency.</p>
  
  <p>example file:</p>
  
  <pre><code>foo apple cat
  1    2    2
  2    3    4
  3    4    6
  ...
  </code></pre>
  
  <p>expected file output:</p>
  
  <pre><code>bar apple cat
  1    2    2
  2    3    4
  3    4    6
  ...
  </code></pre>
  
  <hr>
  
  <p><strong>sed</strong>:</p>
  
  <pre><code>sed -i '1s/foo/bar/g' file
  </code></pre>
  
  <p><code>-i</code> can change the file in place, but this command generate a tmp file on disk and use the tmp file to replace the original one. The io waste time. </p>
  
  <hr>
  
  <p><strong>vim</strong>:</p>
  
  <pre><code>ex -c '1s/foo/bar/g' -c 'wq' file
  </code></pre>
  
  <p>vim doesn't generate a tmp file, but this tool load the whole file in to memory, which waste a lot of time either.</p>
  
  <hr>
  
  <p>Is there a better solution that only read the first row in to memory and write it back to the original file? I known that linux <code>head</code> command can extract the first column very fast.</p>