Tools / Convert newlines to commas
Convert Newlines to Commas
Turn a column of lines into one comma-separated line, ready to paste into a query, a form field or a spreadsheet cell.
How it works
Most systems want a list on one line, not a column. A SQL IN clause, a URL query parameter, a filter box and a spreadsheet cell all expect one string. Copying a column into one of those turns every line break into a space, which is not what you need.
This tool joins the non-empty lines with the separator you choose. Adding a space after the comma produces the readable a, b, c form; turning it off produces the compact a,b,c form that fits better inside code and URLs. Blank lines are dropped rather than producing a,,b, because an empty element in a list is almost never intentional.
Edge cases
- Blank lines are dropped, so the output never contains a doubled separator.
- The separator is literal; choosing a pipe gives
a | bwhich is the usual display form for alternatives. - Text that already contains the separator is not escaped, so check the result before pasting into CSV.
Do it in code instead
The same result with a shell one-liner or a few lines of Python, if you would rather automate it:
Bash (paste)
paste -sd, list.txt
Bash (awk)
awk 'BEGIN{ORS=","} {print}' list.txt
Python
print(', '.join(open('list.txt').read().split()))
JavaScript
text.split('\n').filter(Boolean).join(', ');
Questions
- Can I use a different separator?
- Yes. Comma, semicolon, pipe and space are offered because they cover SQL lists, CSS values, regex alternatives and prose. For anything else, join with a comma and run a find-and-replace on the separator.
- What happens to blank lines?
- They are dropped before joining, so you do not get an empty element. If an empty element is meaningful for your target format, remove the blank lines yourself first so you control exactly where they land.
- Will it wrap the result in quotes?
- No. Quoting is a separate operation. Use the add quotes tool first, or wrap the joined line by hand, because quoting a list and quoting each element produce different results.