Processing a formatted file with awk


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
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 ?

Code:
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)


Code:
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

Moderator's Comments:
Mod Comment Please use code tags to format data

Last edited by jim mcnamara; 1 Week Ago at 09:39 PM..
# 2  
Old 1 Week Ago
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.
# 3  
Old 1 Week Ago
I did not understand everything. But test it.
Code:
awk '
/^PokerStars/   {if (FN) close (FN)
                  FN = gensub(/[^0-9]/, "", "g", $3)
                }
                { print > FN
                }' file

# 4  
Old 1 Week Ago
Not all "awk" implementations have "gensub" function. If the above does not work
Code:
awk '
/^PokerStars/   { if (FN) close(FN)
                 match($3, /[0-9]+/)
                 FN = substr($3, RSTART, RLENGTH)
                }
                { print > FN
                }' file

# 5  
Old 1 Week Ago
No (gen)sub needed:
Code:
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.

Last edited by RudiC; 1 Week Ago at 05:46 AM..
This User Gave Thanks to RudiC For This Post:
nezabudka (1 Week Ago)
# 6  
Old 1 Week Ago
Related, somewhat off-topic

Hi.

I was reading an article in the AI issue of IEEE Spectrum. Perhaps it will be of interest ... cheers, drl
Quote:
Similar claims are being made about a new poker-playing program from a team at the University of Alberta, in Canada, called DeepStack, which has trounced human opponents in Texas hold ’em. The researchers write that it “plays using ‘intuition’ honed through deep learning to reassess its strategy with each decision.” To me, having little poker experience, this was a revelation: Contrary to my naive belief that winning at poker was based on psyching out opponents, poker is really a game of strategy. And the computer has learned a better strategy than we have discovered on our own.
Can Machine Learning Teach Us Anything? - IEEE Spectrum

DeepStack

Texas Hold'em AI Bot Taps Deep Learning to Demolish Humans - IEEE Spectrum
This User Gave Thanks to drl For This Post:
nezabudka (1 Week Ago)
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Help with file processing using awk julearn Shell Programming and Scripting 10 03-06-2016 03:12 PM
awk operation to get table formatted file cheecky Shell Programming and Scripting 2 02-08-2016 12:09 AM
awk - Rename output file, after processing, same as input file High-T UNIX for Dummies Questions & Answers 12 02-01-2015 08:34 PM
awk processing of variable number of fields data file radudownload Shell Programming and Scripting 5 11-16-2013 04:56 AM
awk processing / Shell Script Processing to remove columns text file ajayram Programming 5 03-23-2013 01:51 PM
Help with File Processing (AWK) karumudi7 Shell Programming and Scripting 3 02-11-2012 10:23 AM
Help with File Processing (AWK) karumudi7 Shell Programming and Scripting 1 02-10-2012 09:28 AM
AWK processing of a three-column file chrisjorg Programming 3 01-21-2012 04:01 PM
awk, sed, perl assistance in outputting formatted file abacus Shell Programming and Scripting 13 11-28-2009 01:32 PM
awk help in processing file. ekb Shell Programming and Scripting 3 10-23-2009 10:50 AM
Writing output into different files while processing file using AWK vidyak Shell Programming and Scripting 6 05-04-2009 01:49 PM
poor performance processing file with awk scooter53080 UNIX for Dummies Questions & Answers 2 04-21-2009 10:29 PM
how to change the current file processing to some other random file in awk ? salman4u Shell Programming and Scripting 2 03-02-2009 03:35 AM
Formatted output - awk dhanamurthy Shell Programming and Scripting 3 05-11-2008 11:25 PM
processing a file with sed and awk manouche Shell Programming and Scripting 4 10-11-2007 04:25 PM