How to: Split a CSV and Keep Headers (in Bash)

Find how many lines are within the CSV?

wc -l example.csv

Split the file, based on number of lines:

This will produce a number of files, splitting it based on the number of lines.

split -l 1500000 example.csv

There is one issue, however, the header line (the 1st line) isn’t copied across to the other files. To fix this, we can manually copy it over:

Get the header line:

read -r firstline < example.csv
echo $firstline

Copy the output from above, and paste it instead of MyHeader,Line,Example.

Add header lines:

sed -i '1s/^/MyHeader,Line,Example\n/' example2.csv