title: Concise and portable "join" on the Unix command-line
body: <p>How can I join multiple lines into one line, with a separator where the new-line characters were, and avoiding a trailing separator and, optionally, ignoring empty lines?</p>
  
  <p>Example. Consider a text file, <code>foo.txt</code>, with three lines:</p>
  
  <pre><code>foo
  bar
  baz
  </code></pre>
  
  <p>The desired output is:</p>
  
  <pre><code>foo,bar,baz
  </code></pre>
  
  <p>The command I'm using now:</p>
  
  <pre><code>tr '\n' ',' &lt;foo.txt |sed 's/,$//g'
  </code></pre>
  
  <p>Ideally it would be something like this:</p>
  
  <pre><code>cat foo.txt |join ,
  </code></pre>
  
  <p>What's:</p>
  
  <ol>
  <li>the most portable, concise, readable way.</li>
  <li>the most concise way using non-standard unix tools.</li>
  </ol>
  
  <p>Of course I could write something, or just use an alias. But I'm interested to know the options.</p>