Split a CSV file into multiple files (command line)

A 158 MB order export, 3,000,001 lines, and something downstream that only takes 50 MB at a time — a loader, a mail attachment, or Excel, which stops at 1,048,576 rows per sheet. Cutting the file is easy; keeping the pieces usable is not: every part needs the header, and none may end mid-record.

BigTextFileSplitter splits a text file by line count, by file size or into a fixed number of parts, in the window or from one command line. It reads the CSV as records, not raw lines, so a quoted field holding a line break stays whole.

Three ways to split a CSV, measured

Same file each time: orders-3m.csv, 158.3 MB, 3,000,001 lines, six columns, on Windows 11 (NVMe). Timings are the whole run.

Way Header in each part Quoted field with a line break 158.3 MB run
--by=lines yes kept whole 6 parts, 0.65 s
GNU split -l 500000 no — first part only cut in two 6 parts, 0.28 s
Excel n/a n/a will not open it

Split it from the command line

Same executable, nothing else to install. --keep-header defaults to on: the header goes into every part and is not counted as data.

1. By line count

BigTextFileSplitter.exe split --from=orders-3m.csv --to=parts --by=lines --max-lines=500000

Six files, 1.csv to 6.csv, 500,000 data rows each (500,001 lines with the header), exit code 0, 0.65 s. The cut lands on a record boundary: file 1 ends with a complete row, file 2 starts with the next order_id.

2. By file size

BigTextFileSplitter.exe split --from=orders-3m.csv --to=parts --by=size --max-size=50 --keep-header=1

Four files of 50.0, 50.0, 50.0 and 8.3 MB in 0.60 s, cut between records rather than mid-row. Measured caveat: this mode writes the header to the first file only — even with --keep-header=1, later parts start straight with data. If each part has to be loadable alone, split by lines or by parts.

3. Into a fixed number of parts

BigTextFileSplitter.exe split --from=orders-3m.csv --to=parts --by=count --parts=4

Four files of 253,090 / 248,970 / 248,971 / 248,973 lines, header in all four — when the receiver asks for “four files”, not “50 MB files”.

4. Or in the window

Start → New Task → Split Text File. The header reports what was found (File Size: 134.609 MB, File Lines: 1000001), the preview shows the real column names, and Keep Header In Every File sits next to the line count. 10,000 lines per file: 100 %, Elapsed: 00:00:06.375.

Split Text File window: 134.609 MB, 1000001 lines, UTF-8, column preview, output folder, Max Lines 10000, Keep Header In Every File ticked, 100 % in 00:00:06.375

The output folder afterwards — one CSV per 10,000 records, each with the header:

Explorer: organizations-1000000_split holds 1.csv to 93.csv, one file per 10,000 records

Without this app: what the OS already gives you

Linux and macOS ship split; cmd.exe has no equivalent, so on Windows it is a
PowerShell loop. Both are line based: they cut at the 500,000th line, not between records, so a CSV with a quoted line break inside a field is cut mid-record (measured below).

Linux and macOS

head -n 1 orders-3m.csv > header.csv
tail -n +2 orders-3m.csv | split -l 500000 - part_
for f in part_*; do cat header.csv "$f" > "$f.tmp" && mv "$f.tmp" "$f"; done

Measured on a 3,000,001-line, 124 MB CSV: 0.84 s for six parts of 500,001 lines each, header included. With split -l 1 on a file whose first record holds a quoted newline, one part ends with 1,Alice,"multi and the next starts with line" — correct for lines, broken for records.

Windows: PowerShell, no split in cmd

cmd.exe has no split command, and split.exe only exists if you install Git for Windows
or GnuWin32. Plain PowerShell 5.1 does the same job with a reader/writer pair:

$per = 500000
$r = [IO.StreamReader]::new((Resolve-Path 'orders-3m.csv'))
$hdr = $r.ReadLine()
$i = 0; $n = 0; $w = $null
while (($line = $r.ReadLine()) -ne $null) {
    if ($n % $per -eq 0) {
        if ($w) { $w.Close() }
        $i++
        $w = [IO.StreamWriter]::new("part_$i.csv")
        $w.WriteLine($hdr)
    }
    $w.WriteLine($line)
    $n++
}
if ($w) { $w.Close() }
$r.Close()

Same file, same 500,000 rows per part: 6.1 s against 0.84 s for split. The header is repeated in every part; the line-based caveat is unchanged.

What goes wrong

  • Header and size mode. Only the first part gets it (measured above). A loader that expects a header reads the second part’s first data row as column names — and that row is lost.
  • --keep-header=0 flips the meaning. The header counts as an ordinary line: 1,000,001 lines at 200,000 per file become six files, the last holding one line, the first starting with the header as data.
  • GNU split -l is line based. On the same CSV with a quoted line break it writes 1,"multi at the end of one part and line note" at the start of the next — two files no CSV parser can read. It is the fastest of the three, and the only one that cannot repeat the header.
  • Encoding. The file is read in the encoding you pick (UTF-8 above); a UTF-16 export split as UTF-8 leaves BOM bytes on the first column name.
  • Disk space. The parts need as much free space as the source, which is not deleted. And Excel still will not open a 3 M row part, so cut to what the receiver really handles.

Check the parts before you use them

  1. File count matches the arithmetic: ceil(lines / max-lines), or exactly --parts.
  2. Every part starts with the header (line and count modes) and none ends with a half record.
  3. Row counts add up to the source minus the header, and one part loads — psql \copy, pandas read_csv, or the loader that rejected the 158 MB file.

FAQ

Can I split a CSV from the Windows command line (cmd)?

Yes — the split command above, from cmd.exe or a .bat; check %ERRORLEVEL%. A run that writes nothing exits non-zero.

Does the same command work on macOS and Linux?

Yes, the macOS and Linux builds take the same options; only the executable name differs.

Is the file uploaded anywhere?

No. Source and parts stay on the same disk; there is no upload step and no account.

How large can the source be?

Read and written as a 16 MB-block stream, so size is limited by disk, not memory. The measurement here is 158.3 MB; the same path handles the GB logs this splitter was written for.

Other formats: Split CSV file · split a large TXT file.


More about Withdata BigTextFileSplitter – CSV Splitter


Posted

in

by

Tags: