title: Combine lines with matching first field
body: <p>For a few years, I often have a need to combine lines of (sorted) text with a matching first field, and I never found an elegant (i.e. one-liner unix command line) way to do it. What I want is similar to what's possible with the unix <code>join</code> command, but <code>join</code> expects 2 files, with each key appearing maximum once. I want to start with a single file, in which a key might appear multiple tiles. </p>
  
  <p>I have both a ruby and perl script that do this, but there's no way to shorten my algorithm into a one-liner. After years of unix use, I'm still learning new tricks with <code>comm</code>, <code>paste</code>, <code>uniq</code>, etc, and I suspect there's a smart way to do this. </p>
  
  <p>There are some related questions, like <a href="https://stackoverflow.com/questions/32623035/join-all-lines-that-have-the-same-first-column-to-the-same-line?noredirect=1&amp;lq=1">join all lines that have the same first column to the same line</a>; <a href="https://stackoverflow.com/questions/18493326/command-line-to-match-lines-with-matching-first-field-sed-awk-etc">Command line to match lines with matching first field (sed, awk, etc.)</a>; and <a href="https://stackoverflow.com/questions/17272454/combine-lines-with-matching-keys">Combine lines with matching keys</a> -- but those solutions never really give a clean and reliable solution.</p>
  
  <p>Here's sample input: </p>
  
  <pre><code>apple:A fruit
  apple:Type of: pie
  banana:tropical fruit
  cherry:small burgundy fruit
  cherry:1 for me to eat
  cherry:bright red
  </code></pre>
  
  <p>Here's sample output: </p>
  
  <pre><code>apple:A fruit;Type of: pie
  banana:tropical fruit
  cherry:small burgundy fruit;1 for me to eat;bright red
  </code></pre>
  
  <p>Here's my ideal syntax: </p>
  
  <pre><code>merge --inputDelimiter=":" --outputDelimiter=";" --matchfield=1 infile.txt
  </code></pre>
  
  <p>The "matchfield" is really optional. It could always be the first field. Subsequent appearances of the delimiter should be treated like plain text. </p>
  
  <p>I don't mind a perl, ruby, awk one-liner, if you can think of a short and elegant algorithm. This should be able to handle millions of lines of input. Any ideas? </p>