Processing a formatted file with awk

Hi - I want to interrogate information about my poker hands, sessions are all recorded in a text file in a particular format. Each hand starts with the string <PokerStars> followed by a unique hand reference and other data like date/time. There is then all the information about each hand. My first requirement is to create separate files for each hand (to be interrogated later). Below is an extract of two of the hands to illustrate. What I'd like is to parse the first line, extract the unique hand reference, sub the # and :, this will be the file name, print the first line to the file name and then print all information about that hand to the file name. Then repeat for each hand. So in the examples below two files would be created: 198068432137 and 198068457380 containing each line of text about each hand. Once all the files per hand are created I'll then move onto the next task of extracting common elements of play.
I've written a script in bash but it takes 2 seconds to create each file and there are thousands of hands. If I use awk I can print the first line based on the <PokerStars> string in an instant on the whole file but I'm not sure how to formulate the file name and append all the relevant hand data to it. Can someone please offer a solution using awk or something that is quick to parse ?

PokerStars Hand #198068432137:  Hold'em No Limit ($0.25/$0.50 USD) - 2019/03/12 23:40:28 WET [2019/03/12 19:40:28 ET]
Table 'Hektor III' 6-max Seat #3 is the button
Seat 1: yarfrost ($63.28 in chips)
Seat 2: Manifestorr ($92.50 in chips)
Seat 3: AADublin ($30.80 in chips)
Seat 4: bobster2303 ($37.68 in chips)
Seat 5: Ireful ($50.08 in chips)
Seat 6: trofim-tro ($82.67 in chips)
bobster2303: posts small blind $0.25
Ireful: posts big blind $0.50
*** HOLE CARDS ***
Dealt to bobster2303 [8h Kh]
trofim-tro: folds
yarfrost: folds
Manifestorr: folds
AADublin: calls $0.50
bobster2303: calls $0.25
Ireful: raises $2.50 to $3
AADublin: calls $2.50
bobster2303: folds
*** FLOP *** [9h 5s 9s]
Ireful: checks
AADublin: checks
*** TURN *** [9h 5s 9s] [6d]
Ireful: checks
AADublin: checks
*** RIVER *** [9h 5s 9s 6d] [5h]
Ireful: checks
AADublin: checks
*** SHOW DOWN ***
Ireful: shows [As Qd] (two pair, Nines and Fives)
AADublin: mucks hand
Ireful collected $6.18 from pot
*** SUMMARY ***
Total pot $6.50 | Rake $0.32
Board [9h 5s 9s 6d 5h]
Seat 1: yarfrost folded before Flop (didn't bet)
Seat 2: Manifestorr folded before Flop (didn't bet)
Seat 3: AADublin (button) mucked [7c Qc]
Seat 4: bobster2303 (small blind) folded before Flop
Seat 5: Ireful (big blind) showed [As Qd] and won ($6.18) with two pair, Nines and Fives
Seat 6: trofim-tro folded before Flop (didn't bet)
PokerStars Hand #198068457380:  Hold'em No Limit ($0.25/$0.50 USD) - 2019/03/12 23:41:18 WET [2019/03/12 19:41:18 ET]
Table 'Hektor III' 6-max Seat #4 is the button
Seat 1: yarfrost ($63.28 in chips)
Seat 2: Manifestorr ($92.50 in chips)
Seat 3: AADublin ($27.80 in chips)
Seat 4: bobster2303 ($37.18 in chips)
Seat 5: Ireful ($53.26 in chips)
Seat 6: trofim-tro ($82.67 in chips)
Ireful: posts small blind $0.25
trofim-tro: posts big blind $0.50
*** HOLE CARDS ***
Dealt to bobster2303 [2h 7s]
yarfrost: folds
Manifestorr: folds
AADublin: raises $1 to $1.50
bobster2303: folds
Ireful: folds
trofim-tro: calls $1
*** FLOP *** [6s 9h 3s]
trofim-tro: checks
AADublin: bets $1.55
trofim-tro: folds
Uncalled bet ($1.55) returned to AADublin
AADublin collected $3.09 from pot
AADublin: doesn't show hand
*** SUMMARY ***
Total pot $3.25 | Rake $0.16
Board [6s 9h 3s]
Seat 1: yarfrost folded before Flop (didn't bet)
Seat 2: Manifestorr folded before Flop (didn't bet)
Seat 3: AADublin collected ($3.09)
Seat 4: bobster2303 (button) folded before Flop (didn't bet)
Seat 5: Ireful (small blind) folded before Flop
Seat 6: trofim-tro (big blind) folded on the Flop

Have you written any code? At all?

I do not see where the content of any of the hands is displayed except when one person calls another. I think we need to get a lot more information from you to go any further.

I did not understand everything. But test it.

awk '
/^PokerStars/   {if (FN) close (FN)
                  FN = gensub(/[^0-9]/, "", "g", $3)
                }
                { print > FN
                }' file

Not all "awk" implementations have "gensub" function. If the above does not work

awk '
/^PokerStars/   { if (FN) close(FN)
                 match($3, /[0-9]+/)
                 FN = substr($3, RSTART, RLENGTH)
                }
                { print > FN
                }' file

No ( gen ) sub needed:

awk -F"[#:]" '/^PokerStar/ {if (FN) close (FN); FN = $2} {print > FN}' file

I wouldn't call that a "formatted file", btw. A text file with some repeated (not even periodic!) patterns, perhaps.

1 Like

Hi.

I was reading an article in the AI issue of IEEE Spectrum. Perhaps it will be of interest ... cheers, drl

Can Machine Learning Teach Us Anything? - IEEE Spectrum

DeepStack

Texas Hold'em AI Bot Taps Deep Learning to Demolish Humans - IEEE Spectrum

1 Like