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
Be5-f4 Qf3-c6+ Kf6-f5 Qc6-c8+ Kf5-e5 Rh7-e7+ Ke5-d5 Qc8-d7+ Bf4-d6 Qd7-b5+
Kd5-d4 Qb5-c6 Bd6xe7 Qc6xg6 Kd4-e5 Qg6-e8 Ke5-d6 Qe8-b8+ Kd6-c6 Qb8-c8+
Kc6-d5 Qc8-f5+ Kd5-d4 Qf5xa5 Kd4-c4) +132.53/37 4} Kf5 

Then the expected output:-

59. Rh1 Qe4 60. Qg8+ Kf5 

I tried sed and awk but failed to do so as I'm not at all good with them. :o Please help!

Many Thanks!!

You could try this awk solution:

awk '{print $2} NR==1' RS={ FS="}" ORS= infile

or using sed:

sed ':loop;$!N;$!b loop; :d; s/{[^{}]*}//g; t d' infile
1 Like

Awesome! worked perfectly!! :slight_smile:

Many thanks Chubler_XL :b:

The awk solution dosn't seem to work very well with nested {} 's, this update might be a bit more accurate:

awk '{nest++; nest-=NF-1} nest==1 {print $NF}' RS={ FS="}" ORS= infile

Do you have the option to output your PGN file without computer annotation?

Pardon? What is PGN?
--
Another sed solution that will save some memory, by only reading the needed lines into the input buffer.
Written as a multi-liner (better readable):

sed '
 :loop
 /{/{
  s/{[^{}]*}//
  t loop
  $!N; $!b loop
 }
' infile
1 Like

PGN is Portable Game Notation is used to record chess games. Files are a-h from the queenside to kingside, ranks are 1-8 from White's side to Black's. Pieces are uppercase KQBNR in English and are different for other languages (e.g., German is KDLST). A move or capture (has an x) not starting with an uppercase symbol is a move by a pawn. The long form includes where the piece came from and a dash before where the piece moved to--in the short form this is missing (Nf3) unless a rank or file is required to keep the move from being ambiguous (Ngf3 if there is also a knight on d2).
I'm leaving other info off for sake of brevity, but it's easy to research for those interested.