Issue
This is my original file:
#Game No : 1000693
***** 888poker Hand History for Game 1000693 *****
$1/$2 Blinds No Limit Holdem - *** 18 11 2021 10:41:44
Table DD Poker 9 Max (Real Money)
Seat 7 is the button
Total number of players : 9
Seat 1: Monroe ( $2 )
Seat 2: Mpeti ( $2 )
Seat 3: Bowen ( $2 )
Seat 4: Riccardo ( $2 )
Seat 5: Ramman ( $2 )
Seat 6: Reva ( $2 )
Seat 7: Miklos ( $2 )
Seat 9: Samlet ( $2 )
Seat 10: Geralyn ( $2 )
Hamlet posts small blind [$1]
Geralyn posts big blind [$2]
** Dealing down cards **
Monroe calls [$2]
Mpeti folds
Bowen folds
Riccardo folds
Ramman folds
Reva folds
Miklos calls [$2]
Hamlet folds
** Dealing flop ** [ As, 6s, Ah ]
** Dealing turn ** [ 8s ]
** Dealing river ** [ 4h ]
** Summary **
Monroe shows [ Jh, Td ]
Miklos shows [ Tc, Jc ]
Samlet shows [ Th, 8c ]
Geralyn shows [ Js, Ts ]
Geralyn collected [ $17 ]
For all the dollar values "$X" I need to replace them with "$X/10" (Eks. $2/10=0.2) I am able to extract the dollar values and divide them with 10 with this piped command:
grep -o '\$[[:digit:]]*' dollar.txt | sed 's/[^0-9]//g' | awk '{print $1/10}'
0.1
0.2
0.2
0.2
0.2
0.2
0.2
0.2
0.2
0.2
0.2
0.1
0.2
0.2
0.2
How can I alter the whole original file (or create a new file) so that the rest of the file consist while all the "$X" values are replaced with "$X/10"?
Solution
I would use GNU AWK
for this task following way, let file.txt
content be
#Game No : 1000693
***** 888poker Hand History for Game 1000693 *****
$1/$2 Blinds No Limit Holdem - *** 18 11 2021 10:41:44
Table DD Poker 9 Max (Real Money)
Seat 7 is the button
Total number of players : 9
Seat 1: Monroe ( $2 )
Seat 2: Mpeti ( $2 )
Seat 3: Bowen ( $2 )
Seat 4: Riccardo ( $2 )
Seat 5: Ramman ( $2 )
Seat 6: Reva ( $2 )
Seat 7: Miklos ( $2 )
Seat 9: Samlet ( $2 )
Seat 10: Geralyn ( $2 )
Hamlet posts small blind [$1]
Geralyn posts big blind [$2]
** Dealing down cards **
Monroe calls [$2]
Mpeti folds
Bowen folds
Riccardo folds
Ramman folds
Reva folds
Miklos calls [$2]
Hamlet folds
** Dealing flop ** [ As, 6s, Ah ]
** Dealing turn ** [ 8s ]
** Dealing river ** [ 4h ]
** Summary **
Monroe shows [ Jh, Td ]
Miklos shows [ Tc, Jc ]
Samlet shows [ Th, 8c ]
Geralyn shows [ Js, Ts ]
Geralyn collected [ $17 ]
then
awk 'BEGIN{FPAT="[^0-9]|([0-9]*)";OFS=""}{dollar=0;for(i=1;i<=NF;i+=1){if(dollar){$i*=0.1};dollar=($i=="$")};print}' file.txt
output
#Game No : 1000693
***** 888poker Hand History for Game 1000693 *****
$0.1/$0.2 Blinds No Limit Holdem - *** 18 11 2021 10:41:44
Table DD Poker 9 Max (Real Money)
Seat 7 is the button
Total number of players : 9
Seat 1: Monroe ( $0.2 )
Seat 2: Mpeti ( $0.2 )
Seat 3: Bowen ( $0.2 )
Seat 4: Riccardo ( $0.2 )
Seat 5: Ramman ( $0.2 )
Seat 6: Reva ( $0.2 )
Seat 7: Miklos ( $0.2 )
Seat 9: Samlet ( $0.2 )
Seat 10: Geralyn ( $0.2 )
Hamlet posts small blind [$0.1]
Geralyn posts big blind [$0.2]
** Dealing down cards **
Monroe calls [$0.2]
Mpeti folds
Bowen folds
Riccardo folds
Ramman folds
Reva folds
Miklos calls [$0.2]
Hamlet folds
** Dealing flop ** [ As, 6s, Ah ]
** Dealing turn ** [ 8s ]
** Dealing river ** [ 4h ]
** Summary **
Monroe shows [ Jh, Td ]
Miklos shows [ Tc, Jc ]
Samlet shows [ Th, 8c ]
Geralyn shows [ Js, Ts ]
Geralyn collected [ $1.7 ]
Explanation: I inform GNU AWK
that field is either (0 or more digits) or single any other character using FPAT
. Then I iterate over fields using for
loop, firstly I replace field with 0.1 of value if dollar
variable holds true, then I set dollar
variable to true if field is equal to $
. Order is crucial, as this mean first operation is to field immediately after dollar field. After processing all fields in line I print
it. I set output field separator (OFS
) to empty string, to prevent injecting unwanted characters.
(tested in GNU Awk 5.0.1)
Answered By - Daweo Answer Checked By - Clifford M. (WPSolving Volunteer)