Skip to content

Instantly share code, notes, and snippets.

@bdcravens
Created January 2, 2012 05:44
Show Gist options
  • Save bdcravens/1549500 to your computer and use it in GitHub Desktop.
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
#!/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
@bdcravens
Copy link
Author

Syntax: sh split-csv-save-header.sh [linesperfile]

Lines per file optional, defaults to 1024

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