Tools / Add a comma to the end of each line
Add a Comma to the End of Each Line
Append a comma to every line at once. Handy for SQL IN lists and multi-line arrays, with a clear note on the trailing comma at the end.
How it works
A comma at the end of every line is how most people build a multi-line list before joining it. Paste a column from a spreadsheet, add commas, and the block becomes a comma-separated list without retyping separators.
The catch is the last line. Almost every list format forbids a trailing comma before the closing bracket: JSON rejects it outright, SQL's IN (…) rejects it, and Python tolerates it only inside collection literals. The output here is deliberately uniform, so every line gets a comma including the last one. Delete the final comma, or add a closing bracket on the next line so the list reads correctly.
Edge cases
- The last line also gets a comma. That is intentional for a uniform block but must be removed before JSON or SQL will parse.
- Blank lines are skipped by default so you do not get a stray comma between two newlines.
- Commas inside the original line are left alone; only a trailing comma is added.
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 (sed)
sed 's/$/,/' list.txt
Bash (awk)
awk '{ print $0 "," }' list.txt
Python
print(','.join(open('list.txt').read().split()))
Excel
=A1&","
Questions
- Why does the last line have a comma?
- Because the tool treats every line the same. A trailing comma is invalid in JSON and in most SQL IN lists, so remove the last one, or place the closing bracket on the following line and delete the comma after your final item.
- Can I add a different character instead?
- Yes. The semicolon version does the same operation with a semicolon, which suits CSS and JavaScript statements and SQL statement separators. For arbitrary text use the add suffix tool.
- Does this work for CSV files?
- It adds a comma to the right of each line, which is not the same as building CSV. CSV needs one comma between fields, not one at the end of the record, and fields containing commas need quoting. Use the comma-list tool for that instead.