1

Below an extract of the sample.pgn file

[Event "rated blitz match"]
[Site "Internet Chess Server"]
[Date "2015.08.20"]
[Round "?"]
[White "Villain"]
[Black "Hero"]
[Result "0-1"]
[WhiteElo "1643"]
[BlackElo "1616"]
[ECO "C39"]
[TimeControl "180"]

1.e4 e5 2.f4 exf4 3.Nf3 g5 4.h4 g4 5.Ne5 d6 6.Nxg4 Nf6 7.Nf2
{N} ( 7.Nxf6+ Qxf6 8.Nc3 Nc6 9.d4 Qxd4 10.Bxf4 Qxd1+ 11.Rxd1
Bg4 12.Be2 Bxe2 13.Ke2 
) Bh6 8.Qf3 Nc6 9.c3 Ne5 10.Qe2 Be6 11.d4 Bc4 12.Qc2 Bxf1 13.Rxf1
Ng6 14.Nh3 Ng4 15.Bxf4 Qxh4+ 16.Kd1 Nxf4 17.Nxf4 Ne3+ 18.Kd2
{Villain resigns} 0-1

[Event "rated blitz match"]
[Site "Internet Chess Server"]
[Date "2015.08.20"]
[Round "?"]
[White "Villain"]
[Black "Hero"]
[Result "0-1"]
[WhiteElo "1634"]
[BlackElo "1612"]
[ECO "C30"]
[TimeControl "180"]

1.e4 e5 2.f4 Bd6 {N} ( 2...Nf6 3.fxe5 Nxe4 4.Nf3 Ng5 5.d4 Nxf3+
6.Qxf3 Qh4+ 7.Qf2 Qxf2+ 8.Kf2 Nc6 9.c3 d6 10.exd6 Bxd6 11.Nd2
Be6 12.Ne4 Be7 13.Ng5 Bxg5 14.Bxg5 h6 15.Bh4 g5 16.Bg3 
) ( 2...Nc6 3.Nf3 f5 4.d3 d6 5.Nc3 Nf6 6.g3 g6 7.Bg2 Bg7 8.fxe5
dxe5 9.Bg5 h6 10.Be3 O-O 11.O-O 
) ( 2...exf4 3.Nf3 Nf6 4.e5 Nh5 5.d4 d5 6.Be2 Bg4 7.O-O Be7 8.Ne1
Bxe2 9.Qxe2 g6 10.Nd3 Nc6 11.c3 Qd7 12.Nxf4 Nxf4 13.Bxf4 h6 14.Be3
 )
( 2...d6 3.Nf3 N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RQxX0-9\+\#\!\?]{1,7}|[O\-]{3,5})( |^)\{N\}/ { print $0 }' sample.pgn

but it only print partial information about the games, missing the beginning tags and some other informations:

"]

1.e4 e5 2.f4 Bd6 {N} ( 2...Nf6 3.fxe5 Nxe4 4.Nf3 Ng5 5.d4 Nxf3+
6.Qxf3 Qh4+ 7.Qf2 Qxf2+ 8.Kf2 Nc6 9.c3 d6 1
"]

1.e4 e5 2.f4 Bc5 3.Nf3 d6 4.c3 Nc6 {N} ( 4...Bg4 5.Bc4 Nf6 6.fxe5
Bxf3 7.Qxf3 dxe5 8.d3 Nc6 9.Bg5 a6 1
"]

I don't know how to inform awk that a record is made of 2 blocks (Tags & Game) Can anyone help me to fix this?

1

You can set record separator to

                # empty line
                # empty line
[

just need to escape [ with double backslashes.

After that search for {N} string and if you find one print the whole record adding back \n\n[ if the record is not the first one.

Here is how I would do that:

awk 'BEGIN{RS="\n\n\\["}/\{N\}/{if(NR!=1) printf "\n\n["; print $0}' file

Edit:

To match only black player with {N}:

awk 'BEGIN{RS="\n\n\\["}/\{N\} (\([^)]*\)[[:space:]])*[[:space:]]*[[:digit:]]/{if(NR!=1) printf "\n\n["; print $0}' file
3
  • Thanks a lot jimmij for your time and help, i got it working using this command: awk 'BEGIN{RS="\n\n\["}/([0-9]{1,3}\.)([a-hBNKRQ][a-hBNKRQxX0-9\+\#\!\?]{1,7}|[O\-]{3,5})( |\n|\r)\{N\}/{if(NR!=1) printf "\n\n["; print $0}' sample.pgn
    – Knudz
    Aug 20 '15 at 19:26
  • @Knudz Your command doesn't work for me, but in the meantime I came up with different approach - check the edit of my answer.
    – jimmij
    Aug 20 '15 at 19:34
  • After verification the command that i propose wasn't working for me either. I checked your solution but i'm still not satisfied as it doesn't filter white or black moves correctly. i came up with another solution to match black player: awk 'BEGIN { RS="\\[Event" ; FS="\n"} /( |\.{3}|\n|\r)([a-hBNKRQ][a-hBNKRQxX0-9\+\#\!\?]{1,7}|[O\-]{3,5})( |\n|\r)\{N\}/ {if(NR!=1) printf "[Event"; print $0}' sample.pgn
    – Knudz
    Aug 21 '15 at 17:57
0

Someone posted a response that partially addressed my issue but deleted it a few time later.

awk 'BEGIN{RS="\n\n\\["}/([0-9]{1,3}\.)([a-hBNKRQ][a-hBNKRQxX0-9\+\#\!\?]{1,7}|[O\-]{3,5})( |^|$\n^)\{N\}/{if(NR!=1) printf "\n\n["; print $0}' sample.pgn

The side to move is important. In this case it searches for white moves marked with "{N}". The only problem remaining with this command comes when for instance "white move" and "{N}" are separated with a line breaker like in the first game of this file

2
  • I've posted the answer, but later realized that you want to search for this string only for black side, what significantly complicate the problem. I will undelete my answer temporarily so that you could look and perhaps use some ideas.
    – jimmij
    Aug 20 '15 at 18:02
  • your solution is very good and almost solved my problem but i still can't get my regex to match the "{N}" if there's a line braker between the move and the "{N}". when i open my file with a text editor i can get it to work when checking the multiline box in the search tool box. with this regex: ([0-9]{1,3}\.)([a-hBNKRQ][a-hBNKRQxX0-9\+\!\?]{1,7}|[O\-]{3,5})( |^|$\n^)\{N\}. do you have any ideas on how i can make awk "understand" this multiline regex?
    – Knudz
    Aug 20 '15 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.