Last active
November 10, 2022 09:55
-
-
Save steezeburger/98114746b2e4c5fa1ad1 to your computer and use it in GitHub Desktop.
Bash script for splitting large CSV files into 100 lines while keeping the header.
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
#!/bin/bash | |
FILENAME=file-to-split.csv | |
HDR=$(head -1 ${FILENAME}) | |
split -l 100 ${FILENAME} xyz | |
n=1 | |
for f in xyz* | |
do | |
if [[ ${n} -ne 1 ]]; then | |
echo ${HDR} > part-${n}-${FILENAME}.csv | |
fi | |
cat ${f} >> part-${n}-${FILENAME}.csv | |
rm ${f} | |
((n++)) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks.
A couple of improvements can be done though.