Created
January 2, 2012 05:44
-
-
Save bdcravens/1549500 to your computer and use it in GitHub Desktop.
Splits a folder of CSV files into smaller CSV files, retaining header row
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ ! -d "./backup" ]; then | |
mkdir backup | |
fi | |
lines=1024 | |
for i in *.csv; do | |
split -l ${1:-$lines} $i ${i%.csv}- | |
for j in ${i%.csv}-a*; do | |
if [[ "$j" != *-aa ]] | |
then | |
echo $(head -n 1 $i)|cat - $j > /tmp/out && mv /tmp/out $j | |
fi | |
mv $j $j.csv | |
done | |
mv $i backup/$i | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Syntax: sh split-csv-save-header.sh [linesperfile]
Lines per file optional, defaults to 1024