Skip to content

Instantly share code, notes, and snippets.

@Rittyan
Last active October 7, 2016 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rittyan/009cd305c3d39d96f29a4e9e2cc4e6bc to your computer and use it in GitHub Desktop.
Save Rittyan/009cd305c3d39d96f29a4e9e2cc4e6bc to your computer and use it in GitHub Desktop.
half split csv
#!/bin/sh
# CSVファイルを二分割する
header=`head -n 1 $1`
filename=`basename $1`
dirname=`dirname $1`
echo split_${filename}
# | sed -e "s/^$header"
CNT=0
fileNum=1
lineCount=0
# 行数をカウントして分割する行数を決定
while read line;
do
lineCount=$((lineCount+1))
printf "\r linecount: ${lineCount}:\r"
done < $1
splitLines=$[($lineCount-1)/2]
echo "finish count file lines lines:{$lineCount}"
cd ${dirname}
# もっかい読み込んでファイルを分割
while read line;
do
echo ${line} >> ${filename%.*}_${fileNum}.${filename##*.}
CNT=$((CNT+1))
if [ $CNT = $splitLines ] ; then
fileNum=$((fileNum+1))
echo ${header} > ${filename%.*}_${fileNum}.${filename##*.}
fi
printf "\r fileNum:${fileNum} inputCount : ${CNT}\r"
done < $1
echo "Finished Split CSV!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment