Skip to content

Instantly share code, notes, and snippets.

@patrickserrano
Created May 3, 2019 17:18
Show Gist options
  • Save patrickserrano/0f70d0738b6269d7598a2f65fe76ef05 to your computer and use it in GitHub Desktop.
Save patrickserrano/0f70d0738b6269d7598a2f65fe76ef05 to your computer and use it in GitHub Desktop.
split csv
#!/usr/bin/env bash
# usage: ./split.sh <FILE> <LINES>
FILE_PATH="$1"
FILE_BASENAME="${FILE_PATH##*/}"
FILE_NAME="${FILE_BASENAME%%.*}"
FILE_EXTENSION="${FILE_BASENAME#*.}"
SPLIT_LINES=${$2:-500000}
gsplit --lines=$SPLIT_LINES --numeric-suffixes=1 --suffix-length=3 --additional-suffix=".$FILE_EXTENSION" "$FILE_PATH" "$FILE_NAME-"
for SPLIT in $FILE_NAME-*.$FILE_EXTENSION; do
if [ -z "${SPLIT##$FILE_NAME-001.$FILE_EXTENSION}" ]; then
continue
fi
head -n1 "$FILE_PATH" > temp_file
cat "$SPLIT" >> temp_file
mv -f temp_file "$SPLIT"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment