Skip to content

Instantly share code, notes, and snippets.

@HeathHopkins
Forked from Lazhari/README.md
Created March 9, 2023 00:37
Show Gist options
  • Save HeathHopkins/0c7d011ea49cec0afc79da331c3c90b0 to your computer and use it in GitHub Desktop.
Save HeathHopkins/0c7d011ea49cec0afc79da331c3c90b0 to your computer and use it in GitHub Desktop.
Split a large CSV file and add headers to each file

Split a large CSV file and add headers to each file

Step One: Split file

$ split -l 5000 users.csv ./split-files 

5000 is the number of lines you want for each file.)

Step two: Appending ‘.csv' to each file

$ cd ./split-files
$ for f in *; do echo mv "$f" "$f.csv"; done 

Step three: Adding header to each file

for i in *.csv; do sed -i '' '1i\  
First column name, Second column name, etc, etc  
' $i; done 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment