]> git.sesse.net Git - remoteglot-book/commitdiff
Add some shell magic to split a PGN file into almost equal pieces.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 11 Dec 2014 00:00:47 +0000 (01:00 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 11 Dec 2014 00:00:47 +0000 (01:00 +0100)
partition-pgn.sh [new file with mode: 0755]

diff --git a/partition-pgn.sh b/partition-pgn.sh
new file mode 100755 (executable)
index 0000000..1c73ca4
--- /dev/null
@@ -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