TOOLFRED WORKED EXAMPLE

Split CSV records safely, including quoted newlines

A physical line is not always a CSV record. Try a sample with commas, quotes and a multiline field.

By · Updated · Original examples

THE SHORT ANSWER

Can you split CSV by counting lines?

Not reliably. A quoted CSV field can contain a newline, so one record can span several physical lines. Parse complete records before splitting. The three-record practice file splits into two parts at a limit of two records: IDs 001 and 002 in the first, and 003 in the second, with the header retained in each.

Row-count splitData recordsRetained header
Part 1001 and 002ID, Group, Note
Part 2003ID, Group, Note

Use the three-record practice file

Download the quoted-records CSV. It has one header and three data records. The second record contains a newline inside its quoted Note field.

ID,Group,Note
001,A,"Hello, world"
002,A,"Line one
Line two"
003,B,"A ""quoted"" word"

Split by two records per file

Open the CSV splitter, load the sample and choose a record count of two. Expect two parts: the first contains IDs 001 and 002, and the second contains 003. Each part retains the header. The embedded newline belongs to record 002 and must not become a separate record.

If you split by Group instead, expect an A group containing two data records and a B group containing one. Group labels and output filenames are not a promise that records are unique.

Why a simple line splitter fails

In CSV, a field containing a comma or a newline can be enclosed in double quotes. A double quote within such a field is doubled. Counting physical newline characters therefore does not count records reliably. Correct parsing should recover Hello, world, a Note with two lines, and A "quoted" word as three complete field values.

Check a round trip

Merge the row-count parts in their original order using the CSV merger. Check that the result has three data records, one header and the same field values. Serialization can change line endings or quote style without changing parsed values. Compare parsed records, not only file bytes. You can also inspect columns in the CSV profiler.

Stay within the stated scope

The tool handles bounded UTF-8 CSV files, not arbitrary binary files, Excel workbooks or a database transaction. Read Formats and limits before a larger export. A split does not validate business rules or prevent a spreadsheet from interpreting formula-like content. Review the export settings, keep originals and check the destination’s import requirements.

Questions or corrections

Report a reproducible issue to contact@toolfred.com using an invented example. Avoid sensitive files. How we check tools and maintain examples →

All worked examples →