Re: (SOLVED) Re: fix a txt/dat file with soccer data using awk and sort

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Oct 04, 2015 at 08:52:00AM -0800, Antonio Olivares wrote:
> 
> 
> > -----Original Message-----
> > From: jonfu@xxxxxxxxxx
> > Sent: Sun, 04 Oct 2015 01:01:26 -0400
> > To: users@xxxxxxxxxxxxxxxxxxxxxxx
> > Subject: Re: fix a txt/dat file with soccer data using awk and sort
> > 
> > On Sat, Oct 03, 2015 at 06:23:38PM -0800, 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?
> > 
> > Here is a shot at it.  Assumptions include the team names are
> > longer than shown so I left room for up to 15 chars and for
> > the sorting to work as I have it, the names can not have spaces.
> > 
> > awk '
> > BEGIN { SortCmd = "sort -nr -k 8" }
> > 
> > NR == 1 {
> > 	printf "%-15s %5s %5s %5s %6s %6s %6s %6s\n",
> > 	        "TEAM", "W", "L", "D", "GF", "GA", "DIF", "PTS"
> > }
> > 
> > NR > 1 {
> > 	dif = $5 - $6
> > 	pts = $2 * 3 + $4
> > 	printf "%-15s %5d %5d %5d %6d %6d %6d %6d\n",
> > 		$1, $2, $3, $4, $5, $6, dif, pts | SortCmd
> > }
> > ' datafile
> > 
> This achieves the goal that was set.  The only thing is how can I call it
> from script with a different datafile, instead of writing the datafile
> each time.  ie, save the above instructions to a file called teamstats.sh.
> In it, datafile would be teams1.dat.  How can I call it/script with
> different teams.dat?  so it can run nicely.  
> 

Yes, put it into a file and make the file executable.

To work with different data files, change my "datafile" argument to
"$1" or perhaps "${1:?need a data file name}".  Then you would
execute it as "progname datafile".

BTW personally I avoid naming a program with its language
as an extension.  Your program is "teamstats".  Why do you
care that it is a shell script, a python script, a compiled
C program or ???

For example, suppose in the future you decide to rewrite it
in python.  Now it would be called teamstats.py.  But people
are used to running teamstats.sh.  You have to "retrain" them.
If you leave off the extension no retraining is needed.



-- 
Jon H. LaBadie                 jonfu@xxxxxxxxxx
-- 
users mailing list
users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Fedora Code of Conduct: http://fedoraproject.org/code-of-conduct
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org



[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux