Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 15
An FM (FIDE master) submits games to me in MS word format. Problem is that he doesn't resume labeling black moves with a number after a comment. He enters this: ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2010
    Location
    Rochester, NY USA
    Posts
    9

    fix chess black move number before "..."


    An FM (FIDE master) submits games to me in MS word format. Problem is that he doesn't resume labeling black moves with a number after a comment.

    He enters this: ...e5 34. I want to change it in sed to this: 33...e5 34.

    He enters: {space}{three dots}{a chess move which may be two to five chars}{space}{$move_number}{dot}

    I want to copy and place the {$move_number minus one) in front of the {three_dots} using sed.

    The move numbers and moves are unknown.

    I tried: sed -e 's/ \.\.\.\([\<\>]\) \(*\.\)/ \2...\1/g'

    -but it didn't work. Anybody have any ideas? This is not easy! The reason this occurs is that the black move indicated by "...e5" occurs after a note or comment and he just doesn't resume the move numbering because he inserted the text after the white move, so move numbers are usually not necessary.

  2. #2
    Just Joined!
    Join Date
    Jul 2010
    Posts
    53
    in your sed script you have:
    sed -e 's/ \.\.\.\([\<\>]\) \(*\.\)/ \2...\1/g'
    maybe you want:
    sed -e 's/ \.\.\.\(.*\) \(.*\)\./ \2...\1 \2./g'

    not sure how you get sed to give you move-1 though... maybe awk instead, can use the same regex patterns.

  3. #3
    Just Joined!
    Join Date
    Aug 2010
    Location
    Rochester, NY USA
    Posts
    9
    That doesn't work well, unfortunately.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    This should copy the last to the front:

    Code:
    's/(\.\.\.)([a-z0-9]*) ([0-9]*)\./\3\1\2 \3\./g'
    sed doesn't know how to subtract, so one would have to do a workaround that sed understands. Here a solution that does iterative matching and replacing:

    Code:
    for i in `seq 1 200`; do sed -e 's/^$i/`eval $i + 1`/g'; done
    it may not work out of the box (UNTESTED SNIPPETS)

  6. #5
    Just Joined!
    Join Date
    Aug 2010
    Location
    Rochester, NY USA
    Posts
    9
    This is the input file: ("temp.txt")

    RCC Sat Open, Rochester NY, 12/30/06
    Nikolayev, Igor 2445 - Parry, Matt 2135 G/60
    1. e4 e5 2. Nf3 Nc6 3. c3 d5 [ After David Hayes and Sean Lourette, Matt Parry became another rival to test the Pontziani opening as Black. ] 4. Qa4 Bd7!? 5. ed5 Nd4 6. Qd1 Nf3 7. Qf3 Nf6 8. Bc4 e4 9. Qe2 Bd6 10. d4! [ Many old books consider only 10.d3 here. ] O-O 11. Bg5 h6 12. Bh4 Re8 13. Nd2 e3?! [ Objectively very risky but still well calculated and bold, which means not bad at all in a practical game. ] 14. fe3 Bf4 15. Nf1 [ It’s solid but probably the best was principal 15.Bxf6! Qxf6 16.Rf1 Rxe3 17.Qxe3 Re8! 18.Ne4 Qh4+ 19.Qf2 ( 19.g3 Rxe4! 20.gxh4 Rxe3+ 21.Kd2 Re4+ 22.Kd3 Re3+ 23.Kc2 Bd6! 24.Rf2 Rh3 25.Re1 Rxh4 with compensation for the exchange ) Rxe4+ 20.Kd1 Qh5+ 21.Kc2 Qg6 22.Bd3 Bb5! 23.Rad1 Bxd3+ 24.Rxd3 Re3 25.Rfd1 Qe4 26.R1d2 Re1 27.Qf3 Qxf3 28.Rxf3 Bxd2 29.Kxd2 – with an extra pawn and good winning chances in the rook ending. ] g5 16. Bg3 Bg4 17. Qd3 Qd6?! 18. Kf2! Ne4 19. Kg1 Bg3 20. hg3!? [ Worth of playing since 20.Nxg3 Nxg3 21.hxg3 f5! Is not clear anyway. Plus, 20.hxg3!? keeps in mind a strong tactical idea ] h5?! [ 20…Kg7 is safer ] 21. Re1 Re7 [ On 21…Nxg3? 22.Nxg3 Qxg3 23.Qd2 Black, being a pawn down, is lost due to the exposed king, strong White’s pawn center ] 22. Bb3 Ng3?! [ 22…Rae8 ] 23. e4! [ Sacrificing the exchange, White mobiles his strong pawn center and develops a strong attack. ] Nh1 24. e5! Qd7 25. Bc2?! [ It’s a shame that White’s stumbles just a couple of steps from excelling the game with 25. d6!! cxd6 ( 25…Bf5 26.Qe3 cxd6 27.Qxg5+ Kf8 28.Kxh1 dxe5 29.Ng3 – an easy defeat ) 26.Qg6+ Kh8 27.Qh6+ Kg8 28.Qxg5+ Kf8 29.Qh6+ Kg8 30.Qg6+ Kh8 31.Qf6+ Kg8 32.Ne3! Ng3 33.Nxg4 hxg4 34.e6 fxe6 35.Rxe6 Rf7 36.Re7! ] f5! [ After 25… f6 26. Kh1 Rf8 27. Qg6 Rg7 28. Qh6 fe5 29. de5 White still has the upper hand. ] 26. Kh1 f4 [ Exactly! Under any pressure Matt Parry finds an opportunity to keep chances mutual. The end of the game is performed under time pressure for the both players. ] 27. Nd2 Rf8 28. e6!? Bf5! 29. Qf3 Bc2?! [ 29… Bg4! 30.Qe4 Qd6 31.Nc4 Qa6 32.Qg6+! Rg7 33.Qh6! f3! 34.Re5! f2 35.Bh7+ Kh8 36.Bd3+ Kg8 37.Bf1 Rf5 38.Ne3 Rxe5! 39.dxe5 Qa4 40.d6 cxd6 41.exd6 Qf4 42.Nd5 Qxd6 43.Nf6+ Kf8 44.Qh8+ Ke7 45.Qxg7+ Kxe6 46.Nxg4 hxg4 47.Bc4+ Kf5 48.Qf7+ Ke4 49.Qxf2 ] 30. ed7 Re1 31. Kh2 [ Having advantage and being better on time. ] Re7 [ 31… Bd1 ] 32. d6! cd6? [ 32… Rd7 ] 33.Qd5

    And this is the sed script thus far:
    #!/bin/bash
    clear; cat temp.txt; tr -d '\r' <temp.txt >fixed.pgn; mv fixed.pgn temp.txt; sed -e 's/ / /g' -e 's/[…]/.../g' -e 's/\([0-9]\)\. /\1\./g' -e 's/ [!]/!/g' -e 's/ [?]/?/g' -e 's/0–0–0/O-O-O/g' -e 's/0–0–0/O-O-O/g' -e 's/\[ /[/g' -e 's/ \]/]/g' -e 's/[(] /(/g' -e 's/[(] /(/g' -e 's/ [)]/)/g' -e 's/ [)]/)/g' -e 's/\.\.\. /.../g' -e 's/\[\([0-9]\)/(\1/g' -e 's/\([0-9]\)[!]\]/\1!)/g' -e 's/\([0-9]\)\]/\1)/g' -e 's/\[\([[:alpha:]]\)/{\1/g' -e 's/\([[:alpha:]]\)\([[:alpha:]]\)\.\]/\1\2.}/g' -e "s/[’]/'/g" -e 's/ [–] / {/g' -e 's/\t/ /g' -e 's/\([[:alpha:]]\)\([[:alpha:]]\)\]/\1\2}/g' -e 's/ \([a-h]\)\([a-h]\)\([1-8]\) / \1x\2\3 /g' -e 's/ / /g' -e 's/8Q/8=Q/g' -e 's/\([0-9]\)\.\([a-h]\)\([a-h]\)\([1-8]\)/\1.\2x\3\4/g' -e 's/\([a-z]\)\([a-z]\)[?]\]/\1\2?}/g' -e 's/[)] \([^1-9]\)/) ...\1/g' -e 's/[}] \([^1-9]\)/} ...\1/g' <~/temp.txt >~/fixed.pgn;

    it works perfectly. But I tried the suggested addition:
    -e 's/(\.\.\.)([a-z0-9]*) ([0-9]*)\./\3\1\2 \3\./g'

    and it generated an error: sed: -e expression #31, char 46: invalid reference \3 on `s' command's RHS

    So I tried to fix it by escaping the parens: -e 's/\(\.\.\.\)\([a-z0-9]*\) \([0-9]*\)\./\3\1\2 \3\./g'

    but that didn't change " ...O-O 11." to "11...O-O 11."

    Note that I really need " ...O-O 11." changed to " 10...O-O 11."

    This snippet: sed 's/[^0-9]*\([0-9]*\).*/00\1/;s/.*\(..\)/1\1/' shows that sed can do math, I think.

  7. #6
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    Code:
    's/\(\.\.\.\)\(.*\) \([0-9]*\)\./\3\1\2 \3\./g'
    try this
    Last edited by Kloschüssel; 08-17-2010 at 12:45 PM.

  8. #7
    Just Joined!
    Join Date
    Aug 2010
    Location
    Rochester, NY USA
    Posts
    9
    rmack@totalgym:~$ cat sample.txt
    1.e4 e5 2.Nf3 Nc6 3.c3 d5 4.Qa4 Bd7!? 5.exd5 Nd4 6.Qd1 Nxf3 7.Qxf3 Nf6 8.Bc4 e4 9.Qe2 Bd6 10.d4! {Many old books consider only 10.d3 here.} ...O-O 11.Bg5 h6 12.Bh4 Re8 13.Nd2 e3?! 14.fxe3 {it\'s best to accept} ...Bf4 15.Nf1 rmack@totalgym:~$

    sed -e 's/\(\.\.\.\)\(.*\) \([0-9]*\)\./\3\1\2 \3\./g' <~/sample.txt >~/fixed.pgn;

    rmack@totalgym:~$ cat fixed.pgn
    1.e4 e5 2.Nf3 Nc6 3.c3 d5 4.Qa4 Bd7!? 5.exd5 Nd4 6.Qd1 Nxf3 7.Qxf3 Nf6 8.Bc4 e4 9.Qe2 Bd6 10.d4! {Many old books consider only 10.d3 here.} 15...O-O 11.Bg5 h6 12.Bh4 Re8 13.Nd2 e3?! 14.fxe3 {it\'s best to accept} ...Bf4 15.Nf1

    The sed command found the NEXT ("15") occurrence of the match string and inserted that before O-O.

    The solution you provided is incorrect. But thanks for trying!

  9. #8
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    All code snippets are provided as is without warranty or implied warranty etc etc (see GPLv3 license) ..

    If you need something get done in ways that it works and you are not able to do it, hire a software engineer that can accomplish the task. Just because a software is free and there are a lot of people around that willingly help you with problems, it doesn't mean that they work for you without payment.

  10. #9
    Just Joined!
    Join Date
    Aug 2010
    Location
    Rochester, NY USA
    Posts
    9

    Appreciated!

    Well, I might learn something from your feedback and your code. The correct code would have saved me a few seconds on each game, but I can do it by hand. I appreciate your efforts more than you know and I thank you very much. The elusive solution is probably is probably too complex or simply impossible. Good try, anyway, Kloschüssel!

    -Verlager

  11. #10
    Just Joined!
    Join Date
    Jul 2010
    Posts
    53
    i tried copying the file as you'd posted it - but the ... sequences were saved as special characters (hex: e2 80 a6) instead of 3 periods/points (hex: 2e 2e 2e)

    are you certain in your file that these are actually 3 regular characters that match the \. notation in sed?

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •