Skip to content

Instantly share code, notes, and snippets.

@metric-space
Last active January 8, 2021 05:36
Show Gist options
  • Save metric-space/9e7ce85a1161095d07f2a7dd85ba2273 to your computer and use it in GitHub Desktop.
Save metric-space/9e7ce85a1161095d07f2a7dd85ba2273 to your computer and use it in GitHub Desktop.
split csv into n files with each file containing content as well as csv header
inputfile=$1
filename="${inputfile%.*}"
line_count=$(wc -l < $1)
row_count=$((${line_count} - 1))
movement=$(($row_count/$2))
i=0
j=1
while [ $j -lt $row_count ]
do
# echo $(($j + 1))
# echo $(($j + $movement))
t="${filename}-${i}.csv"
touch $t
head -n 1 $1 > $t
cat $1 | sed -n "$(($j + 1)),$(($j + $movement ))p" >> $t
j=$[$j + movement]
i=$[$i+1]
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment