-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgn2csv.sh
27 lines (20 loc) · 942 Bytes
/
pgn2csv.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/bash
set -e
pgn=${1%.bz2}
csv=${1%.pgn.bz2}.csv
echo "Unzipping"
bzip2 -dfkq $1
echo "Subsetting"
sed -i "/\\[Ev\|\\[Si\|\\[Wh\|\\[Bl\|\\[Re\|\\[UT\|\\[Ti\|\\[Te\|\\[Va/!d" $pgn
echo "Concatenating"
awk '/\[Ev/{if (x)print x;x="";}{x=(!x)?$0:x","$0;}END{print x;}' $pgn > $csv
echo "Normalizing"
sed -i "/BlackTitle/!{s/\\[Ti/\\[BlackTitle \"\"\\],\\[Ti/g}" $csv
sed -i "/WhiteTitle/!{s/\\[BlackTitle/\\[WhiteTitle \"\"\\],\\[BlackTitle/g}" $csv
sed -i "/BlackRatingDiff/!{s/\\[WhiteTitle/\\[BlackRatingDiff \"\"\\],\\[WhiteTitle/g}" $csv
sed -i "/WhiteRatingDiff/!{s/\\[BlackRatingDiff/\\[WhiteRatingDiff \"\"\\],\\[BlackRatingDiff/g}" $csv
echo "Formatting"
sed -i "s/\\[[^\"]*\(\"[^\"]*\"\)\\]/\1/g" $csv
sed -i '1i\"Event","Site","White","Black","Result","UTCDate","UTCTime","WhiteElo","BlackElo","WhiteRatingDiff","BlackRatingDiff","WhiteTitle","BlackTitle","TimeControl","Termination","Variant"' $csv
rm $pgn
echo "Done!"