Page 1 of 1

Looking for a tool

PostPosted: 19 Aug 2009, 16:20
by Volker Pittlik
I have a collection of pgn files sorted and named according to the extended Scid openings classification. In total almost 6000 files.

What I want to do now is to extract a certain number of games from each file into a new file so that I get a collection of a certain number of games of each opening. So far I did not find a tool what can do that automatically. I can do that manually but I'd like to finish before Christmas 2011. If there is nothing around (what at best even runs in linux) I can write something myself but why reinvent the wheel?

Any hints?

tia

vp

Re: Looking for a tool

PostPosted: 19 Aug 2009, 23:33
by Michel
I don't know about a generic tool but if it is a one off-job you may consider writing a small Python script. Since the parsing of the pgn files does not involve chess knowledge (the information is contained in the name) this should be quite trivial.

Re: Looking for a tool

PostPosted: 20 Aug 2009, 07:10
by Zach Wegner
This is very simple for a shell+awk script. This takes the first 10 games of every PGN file in the current directory (*.pgn) and puts them in newfile.pgn.

Code: Select all
for file in *.pgn
do
   awk 'BEGIN { games = 0; state = 0; }
      /^[[]/ { state = 1; }
      /^[^[]/ { state = 2; }
      /^$/ { if (state == 2) { state = 0; games++; } if (games>=10) exit; }
      //' < $file
done > newfile.pgn

Re: Looking for a tool

PostPosted: 20 Aug 2009, 09:31
by Volker Pittlik
Zach Wegner wrote:This is very simple for a shell+awk script....


Thank you very much! Meanwhile I found out it could be done with awk. But finding it out and writing it down are two different things :-).

Volker