Created
May 3, 2019 17:18
-
-
Save patrickserrano/0f70d0738b6269d7598a2f65fe76ef05 to your computer and use it in GitHub Desktop.
split csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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