I need help editing a pgn file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I need help editing a pgn file
# 1  
Old 11-16-2009
MySQL I need help editing a pgn file

I have a bunch of files with the pgn files that represent chess games. I would like to combine the moves into a single line and get rid of the emtpy lines. Then I am planning on importing the games into a database where I can then create a database where I can search for specific games. I was thinking I could use either sed/awk or maybe tr. Thanks in advance.

Code:
[Event "8th Asian Continental Women"]
[Site "Subic Bay PHI"]
[Date "2009.05.23"]
[Round "11"]
[White "Zhu Chen"]
[Black "Zhang Jilin"]
[Result "1/2-1/2"]
[WhiteElo "2491"]
[BlackElo "2335"]
[ECO "B90"]

1.e4 c5 2.Nf3 d6 3.d4 cxd4 4.Nxd4 Nf6 5.Nc3 a6 6.h3 e5 7.Nde2 b5 8.g4 Bb7
9.Bg2 Be7 10.Ng3 O-O 11.O-O Nbd7 12.Nf5 Nc5 13.Bg5 b4 14.Bxf6 Bxf6 15.Nd5 Bg5
16.a3 bxa3 17.b4 Ne6 18.Nxd6 Bxd5 19.Qxd5 Nf4 20.Qxe5 Ne2+ 21.Kh1 Bf4 22.Qd5 Qxd6
23.Qc4 Nd4 24.Rfd1 Rac8 25.Qxd4 Qxd4 26.Rxd4 Be5 27.Rdd1 Bxa1 28.Rxa1 Rc3
29.e5 Rd8 30.Kg1 Rd4 31.Kf1 g5 32.Ke1 Rdc4 33.Bf1 Rxb4 34.Kd2 Rf3 35.Bd3 Ra4
36.Ra2 Rxh3 37.c4 Ra5 38.Kc3 Rxe5 39.Rxa3 Re8 40.Kc2 Rd8 41.f3 Kg7 42.Bf5 Re8
43.Rc3 Rh2+ 44.Kb3 Rb8+ 45.Ka3 Rh1 46.Ka2 Re1 47.Be4 Re8 48.c5 R1xe4 49.fxe4 Rxe4
50.c6 Re8 51.Kb3 h5 52.gxh5 Kh6 53.Rf3 Re7 54.Kc4 Kxh5 55.Kd5 g4 56.Rf1 g3
57.Kd6 Re2 58.Rxf7 Rd2+ 59.Ke6 Rc2 60.Kd7 Kh4 61.c7 g2 62.Rg7 Kh3 63.c8=Q Rxc8
64.Kxc8 a5 65.Rh7+ Kg3 66.Rg7+ Kf2 67.Rf7+  1/2-1/2

# 2  
Old 11-17-2009
OK you show the sample file but what is the expected output ?
# 3  
Old 11-17-2009
Interesting idea. I am waiting for the desired output.

some pre-codes for you.

Code:
 awk '!(/^\[/ || /^$/) {printf "%s ",$0}' urfile.pgn


Last edited by rdcwayx; 11-17-2009 at 01:09 AM..
# 4  
Old 11-17-2009
MySQL

Thanks for the replies. This is the basic format that I am looking for. The awk command just seemed to remove the carriage returns and lines that begin with [.

Code:
^M 1.c4 Nf6 2.Nc3 e6 3.e4 d5 4.e5 d4 5.exf6 dxc3 6.bxc3 Qxf6 7.Nf3 e5 8.Bd3 Nc6^M 9.O-O Bd6 10.Be4 Bf5 11.Qc2 Bxe4 12.Qxe4 Qg6 13.Qd5 O-O-O 14.Qb5 e4 15.Nh4 Qf6^M 16.Nf5 Ne5 17.Ng3 c6 18.Qa4 Qg6 19.Qxa7 Bb8 20.Qc5 Rd3 21.Rb1 h5 22.Qb4 Rd7^M 23.c5 f5 24.Kh1 h4 25.Ne2 Nd3 26.Qc4 Qf6 27.f3 exf3 28.Nd4 fxg2+ 29.Kxg2 Qg5+^M 30.Kh1 h3  0-1^M ^M ^M 1.d4 Nf6 2.c4 e6 3.Nc3 Bb4 4.f3 c5 5.d5 O-O 6.e4 d6 7.Bd3 b5 8.Ne2 exd5 9.cxd5 c4^M 10.Bc2 Qb6 11.Nd4 Na6 12.a3 Bc5 13.Ndxb5 Bd7 14.a4 Nb4 15.Na3 Bf2+ 16.Kf1 Rac8^M 17.Bb1 Nh5 18.g4 Nf6 19.Qe2 Bd4 20.Kg2 Rfe8 21.Qd2 h5 22.g5 Nh7 23.a5 Qc5^M 24.Rd1 Be5 25.f4 Bxc3 26.bxc3 Nd3 27.Bxd3 cxd3 28.Qxd3 Qxc3 29.Qxc3 Rxc3^M 30.Rd4 f5 31.Bd2 Rb3 32.Nc4 Rx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xe4 Bxe4 51.Ba3+ Kf6 52.Kxe4 Kg5 53.Be7+ Nf6+ 54.Ke5 a3 55.Bxa3 Ng4+ 56.Ke6 h4 57.h3 Ne3 58.Bc1 Kh5 59.f6  1-0

# 5  
Old 11-17-2009
If you still need keep the titles, then it will be more shorter.

Code:
awk '{printf (NR<12)?$0"\n":"%s ",$0}' urfile.pgn

# 6  
Old 11-17-2009
Shorter Smilie
Code:
awk '!NF{getline;ORS=FS}1' urfile.pgn

... uff , back to oneliner's Smilie
# 7  
Old 11-17-2009
This works too then:
Code:
awk '!NF{ORS=FS;next}1' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert vi editing to text editing

Dear Guru's I'm using Putty and want to edit a file. I know we generally use vi editor to do it. As I'm not good in using vi editor, I want to convert the vi into something like text pad. Is there any option in Putty to do the same ? Thanks for your response. Srini (6 Replies)
Discussion started by: thummi9090
6 Replies

2. UNIX for Dummies Questions & Answers

File editing

Hi All, I've to edit the below file in the Current File: XXX Rows found with null for one or more non-nullable columns in the source table SELECT COUNT(*) FROM ( SELECT 1 as one FROM datum WHERE:wall: datum.ID IS NULL ) a (1 Reply)
Discussion started by: udayakumar
1 Replies

3. Shell Programming and Scripting

Need help in editing a file

I have a file which has 10 million records in it. When am trying to edit the file with vi, the following error occurs: ~ ~ ~ ~ ~ ~ ~ ~ "file1" Value too large for defined data type Is there any way that I can edit this file without using vi? Any help would be really appreciated.... (8 Replies)
Discussion started by: bobby1015
8 Replies

4. Shell Programming and Scripting

editing line in text file adding number to value in file

I have a text file that has data like: Data "12345#22" Fred ID 12345 Age 45 Wilma Dino Data "123#22" Tarzan ID 123 Age 33 Jane I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes: Data "1012345#22" Fred ID... (16 Replies)
Discussion started by: say170
16 Replies

5. Shell Programming and Scripting

Help with file editing while keeping file format intact

Hi, I am having a file which is fix length and comma seperated. And I want to replace values for one column. I am reading file line by line in variable $LINE and then replacing the string. Problem is after changing value and writing new file temp5.txt, formating of original file is getting... (8 Replies)
Discussion started by: Mruda
8 Replies

6. Shell Programming and Scripting

editing a file

i am writing a script to edit entries in a file. what command will edit a specific file? ex: echo) "what number would you like to change read "number" this is where I am stuck. what command will edit my specific file? (3 Replies)
Discussion started by: gustave
3 Replies

7. Shell Programming and Scripting

editing a file

Hi i have a file name as file1 which has Following content: ROLLOVER_INTERVAL=0 OUTPUT_DIR_COUNT=MULTIPLE FILETYPE=XmlExporter i want to change the FILETYPE=recordexport can any one pls help me on this 2nd query: i want to change OUTPUT_DIR_COUNT=MULTIPLE as ... (2 Replies)
Discussion started by: Aditya.Gurgaon
2 Replies

8. Linux

file editing

I have created a file with vi -x (file name) this is encrypted file when i again open this file it ask me to enter a password before editing it.Can i remove this password but i don't want to delete a file how to do this. Thanks (0 Replies)
Discussion started by: ambavaram
0 Replies

9. Shell Programming and Scripting

Editing file

Hi, I am in a situation wherein am getting file file certailn values suppose 1u56979hhghhklklkkkjkjkjk 0 0 0 The file will have values like above only. I need to add another field of NULL value(of length 9) at the end of first column i.e. It should like this after editing:... (4 Replies)
Discussion started by: rahul303
4 Replies

10. UNIX for Advanced & Expert Users

Editing the end of the file without loading the entire file

hi! I am a newbee. I would really appreciate if you can answer the following question: I have a huge data file, 214MB with several coloumns. I need to delete the very last line of the file. Everything I know takes a lot of time to do it ( because I have to open the file in an editor or run a... (3 Replies)
Discussion started by: Garuda
3 Replies
Login or Register to Ask a Question