Skip to content

Instantly share code, notes, and snippets.

@Lazhari
Created February 25, 2017 22:01
Show Gist options
  • Save Lazhari/7ffa0e8a570726027900e7d0dadb9457 to your computer and use it in GitHub Desktop.
Save Lazhari/7ffa0e8a570726027900e7d0dadb9457 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 
@madurapa
Copy link

madurapa commented Nov 3, 2022

Thanks

@Raahul-Singh
Copy link

for f in *; do echo mv "$f" "$f.csv"; done 

The echo prints the name change instead of renaming it.
It can be simply for f in *; do mv "$f" "$f.csv"; done to rename the csv

@nachomglz
Copy link

It can be simply for f in *; do mv "$f" "$f.csv"; done to rename the csv

Yep, this worked for me, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment