Saturday, September 30, 2023
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 326  / 1 Year ago, mon, july 25, 2022, 3:21:22

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.


More From » command-line

 Answers
0

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.




[#1908] Wednesday, July 27, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sigtestim

Total Points: 298
Total Questions: 108
Total Answers: 106

Location: Bermuda
Member since Thu, Apr 20, 2023
6 Months ago
;