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?
Perhaps a little surprisingly, paste is a good way to do this:
paste
paste -s -d","
This won't deal with the empty lines you mentioned. For that, pipe your text through grep, first:
grep
grep -v '^$' | paste -s -d"," -