-2

I have succeeded in several operations of text files with guidance of you friends. Firstly thank you very much for helping me with commands.

Now, I wish to move further in these operations on text file and one of the places I get stuck is I wish I could remove the * at the end of last line (of every text file within same directory) with either 1/2-1/2 or 1-0 or 0-1

This is the text file content:

enter image description here

  1. Nh3 d5 2. g3 e5 3. f4 Bxh3 4. Bxh3 exf4*

And 1/2-1/2 would represent a draw which would look like this: enter image description here

  1. Nh3 d5 2. g3 e5 3. f4 Bxh3 4. Bxh3 exf4 1/2-1/2

1-0 means White won and it would be like this: enter image description here

  1. Nh3 d5 2. g3 e5 3. f4 Bxh3 4. Bxh3 exf4 1-0

0-1 would mean Black won and would be like this: enter image description here

  1. Nh3 d5 2. g3 e5 3. f4 Bxh3 4. Bxh3 exf4 0-1

Please ignore the * in the title, i only wish to change the content of the text file.

This I wish to do is because by keeping it from any of 1/2-1/2 or 1-0 or 0-1 I will be able to make the chess engine believe that the game was drawn or lost or won by which side. This would be very useful. I thank you very much for helping me. Thanks a lot.

Edit: There are 2000+ files. The reason why I wish to do this is because to find novelties and new lines (never played before) in lesser played variants of the game. This is what I wish to achieve by this operation.

9
  • 3
    HI Maverick! Can you please edit your question and add a little more detail as to what will determine if the text file contains a Win, Lose or Draw? That might help us help you determine what to replace the * with.
    – Terrance
    Feb 22, 2021 at 15:07
  • 3
    ... a testable sample of input and desired output would be helpful Feb 22, 2021 at 15:08
  • 3
    Actual text is preferred to 'images of text' - just a suggestion for the future.
    – Thomas Ward
    Feb 22, 2021 at 15:22
  • 1
    Just going to ask, but is there a reason you're trying to write your own Chess engine on Bash, instead of using an existing chess library/engine in other languages, which will solve the problem you're trying to do already?
    – Thomas Ward
    Feb 22, 2021 at 15:24
  • 1
    That is what we know but the chess engine does not know this. I do not want the engine to know the game outcome. I want it to make the initial moves forcefully for a give particular ECO code thinking of the outcome is so and so what I define in these files.
    – user1166730
    Feb 22, 2021 at 15:34

3 Answers 3

3

Technically you could do this with a call to sed...

# Note we have to escape the slashes to work with regex / sed here.
REPLACEWITH="1\/2-1\/2"  # or "0-1", or "1-0" depending on your intention
sed -i.orig "s/\*$/${REPLACEWITH}/g" FILENAME

... this will find all instances of end of lines with an * on them, and replace them with the end of game notation you want to use. Just make sure if the notation has any / characters in it you replace the / with a \/ so that it's properly escaped for sed and shell to work with.

3
  • 1
    @glennjackman that's there intentionally so you can specify it by REPLACEWITH variable for scripting purposes. The slashes need to be escaped in this case for the sed to recognize it. And I tested the above and it worked fine. Delimiters with the escape that I have above worked fine.
    – Thomas Ward
    Feb 22, 2021 at 18:07
  • Indeed, my mistake. Feb 22, 2021 at 18:09
  • Just change the sed delimiter to something not in the text, like "%", and you don't need to escape the slashes.
    – ubfan1
    Feb 24, 2021 at 17:20
0
+100

With gawk. Install it first:

sudo apt install gawk

You can replace the last character in the line with a space followed by 1/2-1/2 like so:

for f in *.txt
    do
    gawk -i inplace '{gsub(/.$/," 1/2-1/2")}1' "$f"
    done
  • Run the code from within the directory containing your .txt files.

  • You can change what is inside the quotes " 1/2-1/2" notice the leading space after the first quote to for example " 1-0" or " 0-1"

  • You can change .$ to *$ to replace only the last * instead of replacing any last character of the line.

1
  • 1
    Thank you @Raffa you saved me again. :) I have system booting issue right now. I will use this command once I start my system. Your codes always work. Many thanks. :)
    – user1166730
    Feb 25, 2021 at 11:55
0

You can remove the last character in the file if it is * by following this: https://stackoverflow.com/questions/27305177/how-can-i-remove-the-last-character-of-a-file-in-unix. I changed it a bit to replace it with something else:

for f in *.txt; do
    sed -i.bak '$ s/\*$/ 1-0/' $f
done

This adds the text only if * is at the end of the last line, as you specified in your question. It also makes a copy of original files with .bak extension. Remove .bak, if you do not want backup files.

0

You must log in to answer this question.