Our blog

 

Linux Split File (eg CSV) and Keep Header Row

Linux has a great little utility called split, which can take a file and split it into chunks of whatever size you want, eg 100 line chunks.

However, for CSV files etc, each chunk generally needs to have the header row in there. Unfortunately the split command doesn't have an option for that. However with a little bit more code you can achieve it.

CODE:
  1. tail -n +2 file.txt | split -l 4 - split_
  2. for file in split_*
  3. do
  4.     head -n 1 file.txt> tmp_file
  5.     cat $file>> tmp_file
  6.     mv -f tmp_file $file
  7. done

More Reading:

One Comments

admin
October 28th, 2009

note, if the file has line breaks in cell content the split struggles..

I ended up abandoning it

 

 

Leave a Reply