How to Add a Comma (or Any Text) at the End of Each Line

· 5 minutes read

How to Add a Comma (or Any Text) at the End of Each Line

You have a column of values – customer IDs, e-mail addresses, product codes – and you need every line to end with a comma. Or you need each value in quotes for an SQL query, or wrapped in <li> tags for a web page. Editing 300 lines by hand is slow and error-prone. This guide shows the fastest ways to add text to the start or end of each line, whatever tool you have at hand.

The fastest way: an online prefix and suffix tool

Our add prefix and suffix tool does it in your browser, without uploading anything:

  1. Open the add prefix and suffix tool.
  2. Paste your lines into the input box.
  3. Click the comma at end preset – or type your own text into Add to start and Add to end.
  4. If the list goes into code or SQL, tick No comma on the last line.
  5. Click Copy result.

Other presets cover the most common jobs:

  • 'x' – single quotes around each line
  • "x" – double quotes
  • ('x'), – SQL VALUES rows (the last row automatically gets no comma)
  • <li>x</li> – HTML list items

Empty lines are skipped by default, so you won't get stray commas, and Trim spaces first cleans up lists copied from PDFs.

Example:

apple          apple,
banana    →    banana,
cherry         cherry

Add text to each line in Excel or Google Sheets

Spreadsheets do this with a helper column and string concatenation. If your values are in column A, put this in B1 and fill it down:

=A1&","

To add a prefix and a suffix at once:

="'"&A1&"',"

The & operator works the same in Excel and Google Sheets. CONCAT(A1, ",") works too. Afterwards copy column B and use Paste special → Values only so you keep the text, not the formulas.

If what you actually want is all values on one line separated by commas, skip the helper column and use =TEXTJOIN(", ", TRUE, A1:A100) (Excel 2019+ and Google Sheets), or our separator converter.

Notepad++ (Windows)

Notepad++ supports regular expressions in Search → Replace (Ctrl+H):

  1. Set Search Mode to Regular expression.
  2. To add a comma at the end of every line: Find $, Replace with ,.
  3. To add a prefix: Find ^, Replace with your prefix.
  4. To wrap each line in quotes: Find ^(.+)$, Replace with '\1'.

Using .+ instead of .* skips empty lines. Notepad++ also has column mode (Alt + drag) for typing on many lines at once.

VS Code and other editors

In VS Code select the lines, press Shift+Alt+I (Windows/Linux) or Shift+Option+I (macOS) to place a cursor at the end of every selected line, then type the comma. Press Home to jump all cursors to the start of the lines for a prefix.

For regex, open Find and Replace (Ctrl+H), enable the .* button, find ^(.+)$ and replace with '$1', – note that VS Code uses $1, not \1.

Command line (macOS, Linux, WSL)

sed adds text to each line in one command:

sed 's/$/,/' input.txt > output.txt          # comma at the end
sed 's/^/- /' input.txt > output.txt         # prefix "- "
sed "s/.*/'&'/" input.txt > output.txt       # wrap in single quotes
sed '$!s/$/,/' input.txt > output.txt        # comma on every line except the last

On macOS, sed -i needs an empty argument (sed -i '' 's/$/,/' file.txt) when editing in place. With awk: awk '{print "\"" $0 "\","}' input.txt.

Common pitfalls

  • Trailing comma on the last line. SQL IN ('a','b',) and strict JSON fail with a trailing comma. Use the "no comma on the last line" option or sed '$!s/$/,/'.
  • Windows line endings. Files from Windows end lines with \r\n. A naive regex may put the comma after the hidden \r, which looks fine but breaks imports. Our tool normalises line endings; in sed, run dos2unix first.
  • Spaces at the end of lines. apple , is not apple,. Trim the lines first.
  • Quotes inside values. O'Brien wrapped in single quotes becomes invalid SQL. Double the inner quote ('O''Brien') – the separator converter does this automatically when it adds quotes.
  • Empty lines turn into lonely commas. Skip or remove them first.

FAQ

How do I add a comma at the end of each line without the last one?

Turn on No comma on the last line in our tool, or use sed '$!s/$/,/' file.txt on the command line. In a spreadsheet, TEXTJOIN never adds a trailing separator.

How do I add quotes and commas for an SQL IN clause?

Use the 'x' preset with suffix ',, and tick "No comma on the last line". Or paste the list into the separator converter, choose new lines → comma and single quotes – you get 'a', 'b', 'c' on one line.

Can I add text to the beginning of each line in Word?

Word has no per-line replace for plain text, but Find and Replace with wildcards can do it: replace ^p with ^p- adds a prefix after every paragraph break (the first line needs a manual edit). For a plain list it is quicker to use an online tool and paste the result back.

Is it safe to paste confidential data into the online tool?

Yes. The tool runs entirely in your browser; your text is never sent to a server.