Skip to content

Instantly share code, notes, and snippets.

@ersatzryan
Created October 16, 2019 16:19
Show Gist options
  • Save ersatzryan/0079ed96bbaf8018b42bc0458cbb8deb to your computer and use it in GitHub Desktop.
Save ersatzryan/0079ed96bbaf8018b42bc0458cbb8deb to your computer and use it in GitHub Desktop.
CSV Splitter
#!/bin/bash
NUMLINES=4000
while [[ $# -gt 0 ]]; do
case "$1" in
-l|--lines) NUMLINES="$2"; shift; shift;;
-h|--help ) echo -e "Usage: csvsplit [-l line_count] file\n -l, --lines: Number of lines to split\n -h, --help: Show this message"; exit 1;;
-* ) echo "Unknown option: $1"; exit 1;;
-- ) shift; break;;
* ) FILE="$1"; shift;;
esac
done
FILENAME=$(basename $FILE .csv)
tail -n +2 $FILE | split -l $NUMLINES - split_
for split in split_*
do
head -n 1 $FILE > tmp_file
cat $split >> tmp_file
mv -f tmp_file $FILENAME-$split.csv
rm $split
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment