Skip to content

Instantly share code, notes, and snippets.

@rcapile
Last active February 15, 2021 12:54
Show Gist options
  • Save rcapile/f7e57cd2ff229ec3f1c973170cefd941 to your computer and use it in GitHub Desktop.
Save rcapile/f7e57cd2ff229ec3f1c973170cefd941 to your computer and use it in GitHub Desktop.
scripts
#!/bin/bash
set -u
if [ "$#" -ne 2 ]; then
echo "Usage: split-csv.sh file chunks"
exit 1
fi
FILE=$1
CHUNKS=$2
if [ ! -f "$FILE" ]; then
echo "File $FILE not found"
exit 1;
fi
if [ -f temp-tailed ]; then
rm temp-tailed
fi
tail -n +2 "$FILE" > temp-tailed
split temp-tailed -n $CHUNKS -d ${FILE}_
for chunk in ${FILE}_*
do
head -n 1 "$FILE" > tmp_file
cat $chunk >> tmp_file
mv -f tmp_file ${chunk}.csv
done
rm temp-tailed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment