If you are prepared to use standard Linux command-line tools like wc
and grep
then I think my free PGN processor, pgn-extract, will do much of the pre-processing necessary to count games in each category. Below is a basic bash script I put together as a proof of concept. It assumes your file of games is called inputfile.pgn
- adjust as necessary, or pass it in as a command-line argument:
#!/bin/bash
# Output stats on castling.
src=inputfile.pgn
# Pre-process the games to allow textual analysis.
pgn-extract -C -V -N --notags --nochecks --linelength 10000 -s -o stripped.pgn ${src}
# Castling by white: detect a preceding move number.
grep '\. O-O ' stripped.pgn > Wkingside.pgn
grep '\. O-O-O ' stripped.pgn > Wqueenside.pgn
# Castling by black: detect no preceding move number.
grep '[^.] O-O ' stripped.pgn > Bkingside.pgn
grep '[^.] O-O-O ' stripped.pgn > Bqueenside.pgn
# Find games in which no castling occurred.
grep -v ' O-O ' stripped.pgn | grep -v ' O-O-O ' | grep -v '^$' > nocastle.pgn
# Output the FEN of the final position to include castling rights and
# look for the presence of rights.
pgn-extract -C -V -N --notags --nomovenumbers --linelength 10000 -s nocastle.pgn --dropply -1 -F | grep ' [wb] [KkQq]' > rights.txt
# Output the results - assumes valid Result tags in the source.
echo 'Number of games: ' `grep '^\[Result ' ${src} | wc -l`
echo 'W Kingside: ' `cat Wkingside.pgn | wc -l`
echo 'W Queenside: ' `cat Wqueenside.pgn | wc -l`
echo 'B Kingside: ' `cat Bkingside.pgn | wc -l`
echo 'B Queenside: ' `cat Bqueenside.pgn | wc -l`
echo 'Neither: ' `cat nocastle.pgn | wc -l`
echo 'Rights retained: ' `cat rights.txt | wc -l`
I tried the script on the KingBase database (Apr 2019) and, after removing broken games and duplicates got the following stats:
Number of games: 2072354
W Kingside: 1633762
W Queenside: 246158
B Kingside: 1670870
B Queenside: 128124
Neither: 69776
Rights retained: 24280
The 'neither' figure is about 3.4% while the 'rights retained' figure is about 1.2%. If games with 20 or fewer ply are removed then the 'neither' figure reduces to about 2.9% and the 'rights retained' figure to about 0.7%.