title: Replace first two whitespace occurrences with a comma using sed
body: <p>I have a whitespace delimited file with a variable number of entries on each line. I want to replace the first two whitespaces with commas to create a comma delimited file with three columns.</p>
  
  <p>Here's my input:</p>
  
  <pre><code>a b  1 2 3 3 2 1
  c d  44 55 66 2355
  line http://google.com 100 200 300
  ef jh  77 88 99
  z y 2 3 33
  </code></pre>
  
  <p>And here's my desired output:</p>
  
  <pre><code>a,b,1 2 3 3 2 1
  c,d,44 55 66 2355
  line,http://google.com,100 200 300
  ef,jh,77 88 99
  z,y,2 3 33
  </code></pre>
  
  <p>I'm trying to use perl regular expressions in a sed command but I can't quite get it to work. First I try capturing a word, followed by a space, then another word, but that only works for lines 1, 2, and 5:</p>
  
  <pre><code>$ cat test | sed -r 's/(\w)\s+(\w)\s+/\1,\2,/'
  a,b,1 2 3 3 2 1
  c,d,44 55 66 2355
  line http://google.com 100 200 300
  ef jh  77 88 99
  z,y,2 3 33
  </code></pre>
  
  <p>I also try capturing whitespace, a word, and then more whitespace, but that gives me the same result:</p>
  
  <pre><code>$ cat test | sed -r 's/\s+(\w)\s+/,\1,/'
  a,b,1 2 3 3 2 1
  c,d,44 55 66 2355
  line http://google.com 100 200 300
  ef jh  77 88 99
  z,y,2 3 33
  </code></pre>
  
  <p>I also try doing this with the .? wildcard, but that does something funny to line 4.</p>
  
  <pre><code>$ cat test | sed -r 's/\s+(.?)\s+/,\1,/'
  a,b,1 2 3 3 2 1
  c,d,44 55 66 2355
  line http://google.com 100 200 300
  ef jh,,77 88 99
  z,y,2 3 33
  </code></pre>
  
  <p>Any help is much appreciated!</p>