Skip to content

Instantly share code, notes, and snippets.

@kristm
Last active August 29, 2015 14:24
Show Gist options
  • Save kristm/1c555223e0fb4c06f4a8 to your computer and use it in GitHub Desktop.
Save kristm/1c555223e0fb4c06f4a8 to your computer and use it in GitHub Desktop.
splitcsv
#!/bin/bash
if [ $# -lt 1 ]; then
echo -e "Usage:"
echo -e "$0 <csvfile>"
exit
fi
total=$(wc -l $1|awk '{ print $1 }')
inc=4999
index=1
ctr=1
filename=$(echo $1|cut -d. -f1)
ext=$(echo $1|cut -d. -f2)
header=$(head -2 $1)
fio=">"
while [ $index -lt $total ]; do
trange=$((index + inc))
output="${filename}_${ctr}.${ext}"
if [ $ctr -gt 1 ]; then
#we need the headers for each csv
echo "$header" > $output
fio=">>"
fi
if [ $trange -lt $total ]; then
cmd="sed -n '$index,${trange}p' $1 $fio $output"
else
remainder=$((inc - (trange - total)))
cmd="sed -n '$index,$((index + remainder))p' $1 $fio $output"
fi
echo $cmd
eval $cmd
index=$((index + inc + 1))
ctr=$((ctr+1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment