title: Replacing varying delimiters using sed or tr
body: <p>I need to convert a list of IDs from using a delimiter consisting of <code>,</code> and/or <code>\r\n</code> or <code>\n</code> to using <code>,|</code>.  (essentially: <code>s/[,\r\n]+/,\|/g</code> without a trailing <code>|</code>)</p>
  
  <p>Example input data:</p>
  
  <pre><code>123,456,789,012
  </code></pre>
  
  <p>or</p>
  
  <pre><code>123,
  456
  789,
  012
  </code></pre>
  
  <p>and I need the resulting output to be <code>123,|456,|798,|012,</code>: a comma ending each field, and a pipe separating them.</p>
  
  <p>This seems really simple to do, but I'm quite stumped on how to manage this.  I've tried ... quite a few ways, actually, but nothing seems to work.  Here are a few examples:</p>
  
  <ol>
  <li><p><code>sed "s/[,\r\n]+/,\|/g" &lt; filename</code> does not match any of the delimiters.</p></li>
  <li><p><code>sed "s/(,|,?\r?\n?)/,\|/g"</code> does not match anything either.</p></li>
  <li><p><code>tr -t "(,?(\r|\n)+)" ",\|"</code> and <code>tr -t "[,\r\n]+" ",\|"</code> only replace <code>,</code></p></li>
  <li><p><code>tr "(,|\r?\n)" ",\|"</code> works correctly with <code>,</code> but with <code>,\n</code> and <code>,\r\n</code> it replaces the matched characters with multiple bars. Ex: <code>123|||456|||789|||012|</code></p></li>
  <li><p>Getting more complex: <code>sed ':a;N;$!ba;s/\n/,/g"</code> (Taken from <a href="https://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n">here</a>) replaces <code>\n</code> correctly with <code>,</code> but does not work with <code>\r\n</code>.  Replacing the <code>\n</code> with <code>[,\r\n]</code> simply returns the input.</p></li>
  </ol>
  
  <p>I'm stumped.  Can anyone offer some help or advice on this?</p>