From b3c16087f728142f36620ed6be3919e7a551e261 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Thu, 11 Dec 2014 01:00:47 +0100 Subject: [PATCH] Add some shell magic to split a PGN file into almost equal pieces. --- partition-pgn.sh | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 partition-pgn.sh 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 -- 2.39.2