fix a txt/dat file with soccer data using awk and sort

Stephen Davies sdavies at sdc.com.au
Sun Oct 4 06:41:01 UTC 2015


On 04/10/15 12:53, Antonio Olivares wrote:
> Dear fedora users,
>
> I have a file table.dat with team data ie, Wins Loses Draws Goals For, Goals Against, Total Points as follows:
>
> $ cat table.dat
> Team    W       L       D       GF      GA      DIF     PTS
> Team1   3       2       1       13      17
> Team2   2       3       1       14      13
> Team3   6       0       0       28      13
> Team4   0       6       0       5       23
> Team5   0       0       0       0       0
> $ awk '{print $1 "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 "\t" $7 "\t" $8 "\t" $5-$6, "\t" $2*3+$3*0+$4*1}' table.dat
> Team    W       L       D       GF      GA      DIF     PTS     0       0
> Team1   3       2       1       13      17                      -4      10
> Team2   2       3       1       14      13                      1       7
> Team3   6       0       0       28      13                      15      18
> Team4   0       6       0       5       23                      -18     0
> Team5   0       0       0       0       0                       0       0
> bash-4.3$
>
> I can get the DIF by subtracting the 5th - 6th and get the goal differential, and the points by multiplying the Wins by 3 and the loses by 0 and the ties by 1 and get the points.  I am not expert, but instead of using a spreadsheet I would like to use awk as the example shows, but I would like the DIF to be under DIF and the points under PTS, how can I accomplish this?  Also if it were possible which I do not see why not?  is how can I sort the teams by the ones higher in the table?
>
> For example, I would like to do something like:
>
> http://www.premierleague.com/en-gb/matchday/league-table.html/
>
> http://www.mlssoccer.com/standings
>
> http://www.mediotiempo.com/tabla_general.php?id_liga=1
>
> but only using awk/sed/sort no spreadsheet, no database only nice unix/linux/bsd tools
>
> Also add a variation, if the teams tie in regulation, then overtime kicks(extra time) and/or penalty kicks to determine a winner.  If the team wins in overtime or penalty kicks the winning team earns two points and the loser earns one point only
>
> Team    W       L       D       GF      GA      OT/PKS   DIF     PTS
> Team1   3       2       1       13      17
> Team2   2       3       1       14      13
> Team3   6       0       0       28      13
> Team4   0       6       0       5       23
> Team5   0       0       0       0       0
>
> Here Team1 ties with Team2 and they go into overtime and remain tied in Overtime.  After the overtime, they go into Penalty Kicks.  Team2 beats Team1 in PKS and earns two points, in the overall PTS
>
> team1 earns 10 total pts, and team2 should have 8 pts.  But the awk command on top gives 7 points because it does not take into account PKS.
>
> awk '{print $1 "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 "\t" $7 "\t" $8 "\t" $5-$6, "\t" $2*3+$3*0+$4*1}' table.dat
>
> how can it be done so that the table prints out correctly and in the OT/PKS line, the team that wins gets a 1:0 and the losing team gets a 0:1 and each time they tie and go in to OT, a running tally gets going 2:0 or 1:1 depending if they split the games.
>
> Thank you in advance for suggestions and advice.  I am discovering awk that one can do math to lists and tables it is awesome.  I did not know this, I just used sed -i 's|*|x|g' file to replace text x with *.
>
> Best Regards,
>
>
> Antonio
>
> ____________________________________________________________
> Send your photos by email in seconds...
> TRY FREE IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if3
> Works in all emails, instant messengers, blogs, forums and social networks.
>
>
One way could be as follows:

script:
#!/bin/sh
gawk -f dat.awk dat
gawk -f dat2.awk dat | sort -t' ' -k8n

dat.awk:
NR==1   {print $1 "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 "\t" $7 "\t" $8;exit}

dat2.awk:
NR==1   {next}
         {print $1 "\t" $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 "\t" $5-$6 "\t" 
$2*3+$3*0+$4*1}

Result:
Team    W       L       D       GF      GA      DIF     PTS
Team4   0       6       0       5       23      -18     0
Team5   0       0       0       0       0       0       0
Team2   2       3       1       14      13      1       7
Team1   3       2       1       13      17      -4      10
Team3   6       0       0       28      13      15      18

You should also be able to set OFS to tab to eliminate the "\t" literals.


More information about the users mailing list