From: Steinar H. Gunderson Date: Thu, 11 Dec 2014 00:00:47 +0000 (+0100) Subject: Add some shell magic to split a PGN file into almost equal pieces. X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=b3c16087f728142f36620ed6be3919e7a551e261;p=remoteglot-book Add some shell magic to split a PGN file into almost equal pieces. --- diff --git a/partition-pgn.sh b/partition-pgn.sh new file mode 100755 index 0000000..1c73ca4 --- /dev/null +++ b/partition-pgn.sh @@ -0,0 +1,31 @@ +#! /bin/sh +FILE=$1 +P=$2 +NUM=$3 + +split_point() { + FILE=$1 + P=$2 + NUM=$3 + SIZE=$( stat -c %s "$FILE" ) + + if [ "$P" -eq "0" ]; then + echo 0 + elif [ "$P" -eq "$NUM" ]; then + echo $SIZE + else + TENTATIVE_SPLIT_POINT=$(( SIZE * P / NUM )) + OFFS=$( tail -c +${TENTATIVE_SPLIT_POINT} "$FILE" | grep -b '^$' | head -n 1 | cut -d: -f1 ) + if [ -z "$OFFS" ]; then + echo $SIZE + else + echo $(( TENTATIVE_SPLIT_POINT + OFFS )) + fi + fi +} + +FROM=$( split_point "$FILE" $P $NUM ) +TO=$( split_point "$FILE" $(( P + 1 )) $NUM ) +SIZE=$(( TO - FROM )) + +tail -c +$FROM "$FILE" | head -c $SIZE