Tool for truncate pgn files

Discussion of anything and everything relating to chess playing software and machines.

Moderators: hgm, Rebel, chrisw

Elorejano
Posts: 101
Joined: Sat Mar 20, 2010 3:31 am

Tool for truncate pgn files

Post by Elorejano »

Hi!
After all sorry for my bad english.
I neede a tool for to truncate pgn files.
Ie: if i have games untill 60 or 70 moves, if its possible to cut the moves to 30 moves?
Moreover, its posible to change all the tags as below?

[Event "New York (Game 23)"]
[Site "?"]
[Date "1993.??.??"]
[Round "?"]
[White "Yermolinsky"]
[Black "Kaidanov"]
[Result "1-0"]
[ECO "D01"]

to

[Event "?"]
[Site "?"]
[Date "????.??.??"]
[Round "?"]
[White "?"]
[Black "?"]
[Result "1-0"]
[ECO "D01"]

If Extract make this, could you provide me the exact sintaxis?
Thanks in advance
User avatar
RJN
Posts: 303
Joined: Fri Jun 21, 2013 5:18 am
Location: Orion Spiral Arm

Re: Tool for truncate pgn files

Post by RJN »

https://www.cs.kent.ac.uk/people/staff/djb/pgn-extract/
https://www.cs.kent.ac.uk/people/staff/ ... /help.html

Setting bounds on the number of moves in a game (-b)
The -b flag allows you to select games which have a number of moves within the bounds you set. You can set a lower bound on the number of moves by using -bl ('l' = lower bound), or an upper limit by using -bu ('u' = upper bound). Both are followed by the number of moves so

pgn-extract -bu20 file.pgn
will find brevities of 20 moves or less, whilst

pgn-extract -bl60 file.pgn
will find games of 60 moves or move. Bounds may be combined so

pgn-extract -bl30 -bu40 file.pgn
will find games in the range [30..40] moves. If neither 'l' nor 'u' is used, but just a number following the -b, this means that the number of moves must exactly match that number. Alternatively, 'e' can be used to stand for 'equal to'. The following are equivalent and find all games of exactly 35 moves.

pgn-extract -b35 file.pgn
pgn-extract -be35 file.pgn


Limiting the number of plies (>= 0) output (--plylimit)
The number of moves (actually plies) output for a game can be limited by using --plylimit.
This must be followed by the maximum number of plies to be output for a game. For instance,

pgn-extract --plylimit 10 --nomovenumbers --notags file.pgn
will output games up to a maximum of 10 plies (including variation lines), without game tags and no line numbers.

Note: If the game has not ended before the ply limit is reached then * will be used as the terminating result to indicate an incomplete game (see --noresults for how to suppress this.)
i7-5930K @4.5GHz, H100i Hydro Cooler, 64GB DDR4 Corsair Dominator Platinum @3000MHz, ASUS X99 Deluxe mboard, 1TB EVO 850 SSD
Elorejano
Posts: 101
Joined: Sat Mar 20, 2010 3:31 am

Re: Tool for truncate pgn files

Post by Elorejano »

Thx, y will try
Elorejano
Posts: 101
Joined: Sat Mar 20, 2010 3:31 am

Re: Tool for truncate pgn files

Post by Elorejano »

Hi!
Truncate works fine but i couldnt change the tags.
With
--notags
tags disapears and i need them
Any help?
Thanks in advance
User avatar
Ozymandias
Posts: 1535
Joined: Sun Oct 25, 2009 2:30 am

Re: Tool for truncate pgn files

Post by Ozymandias »

Elorejano wrote:i couldnt change the tags.
With
--notags
tags disapears and i need them
=========================(45) tagNull ==============================
"tagNull" replaces all values of a user-specified tag with the appropriate null value.
Unless specified below, the null value is "?".
For the "Date" tag and the "EventDate" tag, the null value is "????.??.??".
For the "Result" tag, the null value is "*". Also the result value at the end of the "MoveText Section" is set to "*".
For the "Time" tag, the null value is "??:??:??". For the "SetUp" tag, the null value is "0".
For the "FEN" tag, the null value is "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", the standard opening position.
Changing to a null value could make the PGN game description illegal or inaccurate. Especially if you change the "FEN" tag or the "Result" tag.
If you are not satisfied with the null value inserted by "tagNull", you can use a text editor to search and replace all instances of that tag with a new tag having a new value.
Syntax: tagNull filename.pgn tag_name Example: tagNull alpha.pgn Round Output: outH.pgn
Comments:
1. "tag_name" values are case-sensitive.
2. An alternative to inserting null values is to remove tags using "tagRemove" or "tagOrder".

http://www.40h.netai.net
Norm Pollock
Posts: 1056
Joined: Thu Mar 09, 2006 4:15 pm
Location: Long Island, NY, USA

Re: Tool for truncate pgn files

Post by Norm Pollock »

To cut the length to maximum 30 moves use truncate.exe from the above link.

truncate pgnfile.pgn 60 where 60 is the number of plies in 30 moves

As pointed out above, tagNull from the above link will remove all tag values.

The ECO values are subjective and can be re-installed using SCID or pgn-extract for example. However, do not expect the same results from different software since ECO is subjective.

The result values can be reinstalled following this procedure:

To retain "Result" tag values, first use "resultSplit", then use "tagNull" on each output file, and then use a text editor to replace "*" with the appropriate result.

truncate, resultSplit and tagNull all in the link from the previous post.
User avatar
Ajedrecista
Posts: 1971
Joined: Wed Jul 13, 2011 9:04 pm
Location: Madrid, Spain.

Re: Tool for truncate PGN files.

Post by Ajedrecista »

Hello Carlos:
Elorejano wrote:Hi!
After all sorry for my bad english.
I neede a tool for to truncate pgn files.
Ie: if i have games untill 60 or 70 moves, if its possible to cut the moves to 30 moves?
Moreover, its posible to change all the tags as below?

[Event "New York (Game 23)"]
[Site "?"]
[Date "1993.??.??"]
[Round "?"]
[White "Yermolinsky"]
[Black "Kaidanov"]
[Result "1-0"]
[ECO "D01"]

to

[Event "?"]
[Site "?"]
[Date "????.??.??"]
[Round "?"]
[White "?"]
[Black "?"]
[Result "1-0"]
[ECO "D01"]

If Extract make this, could you provide me the exact sintaxis?
Thanks in advance
You can use regular expressions regarding PGN tags. You can open the PGN file with Notepad++ and use regular expressions like in this stackoverflow thread. Hence you can keep Result and ECO tags unchanged.

Once you have opened the PGN file with Notepad++, you click on Search\Replace... (shortcut Ctrl+H) and type the following:

Code: Select all

Find what: Event "(.*)"
Replace with: Event "?"
Click 'Regular expression' radio button in 'Search Mode' and finally click on 'Replace All' button. The string (.*) has worked for me so far with your example of the original post. So you can do this more times with the rest of tags that you want to change. Good luck!

Code: Select all

Find what: Event "(.*)"
Replace with: Event "?"

Find what: Site "(.*)"
Replace with: Site "?"

Find what: Date "(.*)"
Replace with: Date "????.??.??"

Find what: Round "(.*)"
Replace with: Round "?"

Find what: White "(.*)"
Replace with: White "?"

Find what: Black "(.*)"
Replace with: Black "?"
It probably can be made automatically (all at once, not string by string) using a batch file .bat (@echo off and these things) but I do not know the syntaxis.

Last but not least, make sure that you work with a copy of your PGN file, just in case something goes wrong. You still have the original PGN in this case.

Regards from Spain.

Ajedrecista.
Norm Pollock
Posts: 1056
Joined: Thu Mar 09, 2006 4:15 pm
Location: Long Island, NY, USA

Re: Tool for truncate PGN files.

Post by Norm Pollock »

I'm thinking about modifying "tagNull" to replace tag values with a null value one tag type at a time. Perhaps the current way of replacing ALL tag values with a null value in one shot is too extreme.
User avatar
Ozymandias
Posts: 1535
Joined: Sun Oct 25, 2009 2:30 am

Re: Tool for truncate PGN files.

Post by Ozymandias »

Norm Pollock wrote:I'm thinking about modifying "tagNull" to replace tag values with a null value one tag type at a time. Perhaps the current way of replacing ALL tag values with a null value in one shot is too extreme.
You mean that if I write "tagNull alpha.pgn Round", is not just the "Round" tag that gets modified? I seem to recall it was.
Norm Pollock
Posts: 1056
Joined: Thu Mar 09, 2006 4:15 pm
Location: Long Island, NY, USA

Re: Tool for truncate PGN files.

Post by Norm Pollock »

Ozymandias wrote:
Norm Pollock wrote:I'm thinking about modifying "tagNull" to replace tag values with a null value one tag type at a time. Perhaps the current way of replacing ALL tag values with a null value in one shot is too extreme.
You mean that if I write "tagNull alpha.pgn Round", is not just the "Round" tag that gets modified? I seem to recall it was.
Ooops!!! You are correct. My bad. The current version replaces tag values with a default value, ONE TAG TYPE AT A TIME. Please ignore my previous comments about tagNull. However my comment about "truncate" was correct.

Therefore, Carlos should NOT replace tag values with defaults for ECO and Result.