Use sed 's/^/"/; s/$/"/' input.txt > quoted.txt to add one double quote to the beginning and end of every line while leaving the original file unchanged.

This is literal line wrapping. It does not automatically turn the result into valid JSON, SQL, or CSV, because embedded quotes and the final list punctuation may need format-specific treatment.

sed add quotes to each line
Sed Add Quotes to Each Line: Commands and Escaping

Add quotes to every line with sed

  1. Save the plain list in a file such as items.txt, with one item on each line and no quotes around the values yet.
  2. Run sed 's/^/"/; s/$/"/' items.txt > quoted.txt from the same directory. The first substitution inserts a quote at the start of each line, and the second inserts a quote at the end.
  3. Open quoted.txt to check the result. Text between the new quotes remains unchanged, while a blank source line becomes "" because the command applies to every line.
  4. Use in-place editing only when that is acceptable. GNU sed supports sed -i 's/^/"/; s/$/"/' items.txt; macOS and other BSD versions generally require a backup suffix, such as sed -i.bak 's/^/"/; s/$/"/' items.txt.

Choose double or single quotes

The quote character is part of the substitution rather than a setting supplied by sed. Double quotes are convenient for JSON-like output, while single quotes are commonly used for SQL string literals.

  • Double quotes: sed 's/^/"/; s/$/"/' items.txt > quoted.txt
  • Single quotes: sed "s/^/'/; s/\$/'/" items.txt > quoted.txt

The single-quote version uses double quotes around the shell argument because the sed script itself contains single quotes. The backslash before $ prevents the shell from expanding the end-of-line anchor before sed receives it.

These commands add wrappers only. For example, an item containing say "hi" will receive outer double quotes, but the embedded quotation marks will still need escaping before the value is safe to paste into JSON.

Add commas without a broken final entry

Lists often need a delimiter between values but no delimiter after the last value. In a sed address, $! means “every line except the last,” so it can append a comma without leaving a trailing comma.

For a literal, comma-separated JSON-style structure, use:

sed '1s/^/[/; s/^/"/; s/$/"/; $!s/$/,/; $s/$/]/' items.txt > array.json

The first rule adds the opening bracket to the first line, the quote rules wrap every value, the comma rule runs on every line except the last, and the final rule adds the closing bracket. The resulting structure resembles:

["first item","second item","last item"]

A trailing comma is omitted because a JSON array or SQL IN list with one is a syntax error in most contexts. This command is still only doing literal wrapping, so it must be modified when the values contain characters that the destination format reserves.

Escape characters for the destination format

JSON, SQL, and CSV do not use the same escaping rules. A quotation mark that is safe inside one format can break another. Match the substitutions to the format that will consume the output.

JSON

JSON requires an embedded double quote to receive a backslash escape and requires an embedded backslash to be doubled. Escape the backslashes first so the backslashes added for quotation marks are not doubled a second time.

sed 's/\\/\\\\/g; s/"/\\"/g; 1s/^/[/; s/^/"/; s/$/"/; $!s/$/,/; $s/$/]/' items.txt > array.json

This handles embedded double quotes and backslashes and creates the surrounding array. It does not escape raw control characters such as tabs, so the complete command should not be treated as a general-purpose JSON validator.

SQL

SQL string literals use a different rule: an embedded single quote is doubled. The behavior matches the string-constant rules described in PostgreSQL’s lexical structure documentation.

sed "s/'/''/g; 1s/^/(/; s/^/'/; s/\$/'/; \$!s/\$/,/; \$s/\$/)/" items.txt > sql-values.txt

The output is a parenthesized list such as ('first item','second item'), ready to place after IN in a query. The escaping rule changes an embedded apostrophe from ' to '', which is different from JSON’s backslash treatment.

CSV

For CSV, fields are wrapped in double quotes and an embedded double quote is doubled. The field-quoting rule comes from RFC 4180.

sed 's/"/""/g; s/^/"/; s/$/"/' items.csv > quoted.csv

This command treats each source line as a quoted field and emits the fields on separate lines. It does not decide whether the source lines represent fields in one record or separate records, so that distinction must be made before using the output as a CSV row.

Destination Embedded-quote rule Expected structure
JSON array Double quote receives a backslash; backslash is doubled Bracketed array with no trailing comma by default
SQL IN list Embedded single quote is doubled Parenthesized values with no trailing comma by default
CSV row Embedded double quote is doubled Quoted fields joined with commas
Plain quoted lines Depends on the selected quote and escaping controls One wrapped line for each input line

Build format-aware output in your browser

When the output must be a complete JSON array, SQL IN clause, or CSV row, use the Add Quotes to Each Line browser tool instead of assembling every part by hand.

  1. Paste the plain list into the input area, keeping one item on each line.
  2. Click the preset for JSON array, SQL IN clause, CSV row, or plain quotes.
  3. Fine-tune the quote style, delimiter, trailing delimiter, escaping, blank-line skipping, or per-line trimming when the source requires it.
  4. Copy the generated result and paste it into the editor, query console, or spreadsheet import.

The JSON preset follows RFC 8259, the SQL preset applies the standard doubled-single-quote rule, and the CSV preset applies the RFC 4180 field rule. The CSV preset emits the complete row on a single line. A trailing delimiter is off by default because most JSON and SQL insertion contexts do not want one, and the result reports how many lines were processed.

Everything runs in the browser: the input is not uploaded, stored, or attached to an account. The input limit is one million characters, processed in a single linear pass. Running the tool on its own output wraps it again, so begin with the original plain list whenever you need to change the output format.

Avoid common sed mistakes

  • Do not redirect back into the input file. A command such as sed ... items.txt > items.txt can truncate the file before sed reads it. Write to another filename or use the platform-specific -i option.
  • Do not treat quoting as escaping. Adding a quote around a value does not protect an embedded quote of the same kind. Apply the format-specific rule before wrapping the value.
  • Do not add a final comma automatically. Use the last-line exception when the destination expects a clean JSON array or SQL IN list.
  • Do not assume every line is a CSV field or record. Line breaks usually separate CSV records, while commas separate fields. Decide which meaning matches the source before joining the values.
  • Do not reuse already wrapped output. A quotation mark may be part of the original content or may be wrapping added by an earlier run. Starting from the original list prevents accidental double-wrapping.

Related reading: Extract Hyperlinks from Word: Command Line vs Online.

Related reading: Convert \n to New Line Cheat Sheet.