Arsen Zahray
Arsen Zahray

Reputation: 25287

Split csv file into multiple files

I have a single huge csv file that contains header line and than hundreds of thousands of records.

I want to split it into multiple files, each containing the same header and than 10.000 records or what's left

If I didn't care about the header, I'd do split -l 10000 myfile. However, I need each file to contain the header

How do I do this?

Upvotes: 0

Views: 319

Answers (1)

larsks
larsks

Reputation: 311347

Split the file, exluding the header:

tail -n +2 myfile | split -l 10000 - prefix-

Get the header line:

head -1 myfile > header

And then append it to all the generated files:

for file in prefix-*; do
   cat header $file > $file.new
   mv $file.new $file
done

Upvotes: 3

Related Questions