Last active
September 4, 2020 01:35
-
-
Save zianwar/7267b2ab982c987707839cb7aa0ea52f to your computer and use it in GitHub Desktop.
Split CSV file into multiple files by specifying how many lines in each split.
This file contains hidden or 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
#!/bin/bash | |
# This script uses the GNU split utility: http://man7.org/linux/man-pages/man1/split.1.html. | |
# You can use it as a bash script or copy the `splitcsv` function into your own bash/zsh... profile. | |
function splitcsv { | |
if [ "$#" -ne 2 ]; then | |
printf "Usage:\n\n splitcsv [filename] [lines] \n" | |
return 1 | |
fi | |
local filename=$1 | |
local lines=$2 | |
header=$(head -n 1 $filename) | |
split -l $lines $filename | |
i=1 | |
for x in `ls x* | sort` | |
do | |
# ensure csv headers are added to each split | |
if [ "$(head -n 1 $x)" != "$header" ] | |
then | |
echo -e "$header\n$(cat $x)" > $x | |
fi | |
# rename csv file | |
mv $x ""$(basename $1 .csv)"_$i".csv | |
i=$(($i+1)) | |
done | |
} | |
splitcsv $1 $2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment