Sorting from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting from file
# 1  
Old 01-08-2016
Sorting from file

Im trying to write a script to make a hockey-table.

I have a textfile containing information from a previous season like this

Example

Code:
Round:1
20020923:Leksands IF-Brynäs IF:2-3 : (1-1,0-1,1-1):6298:Leksands Isstadion

20020924:Linköpings HC-Djurgårdens IF:3-0 : (0-0,2-0,1-0):4587:Stångebro Ishall

Fields are seperated by : first is the date , second is team-team, third is score, fourth is period-score, fifth is spectators ,last is arena.

I wanna write a script to sort the txt file and get a output kinda like this.

Code:
"Points" "Team"    "Matches"  "Wins"  "Ties" "Losses"

I know how to write a script to get the graphic looking good, but i have no clue how to sort the txt file.

Any tips would be appriciated!

Last edited by Don Cragun; 01-09-2016 at 12:39 AM.. Reason: Add CODE tags.
# 2  
Old 01-08-2016
What program are you using for scripting?

Code:
Round:1
20020923:Leksands IF-Brynäs IF:2-3 : (1-1,0-1,1-1):6298:Leksands Isstadion

20020924:Linköpings HC-Djurgårdens IF:3-0 : (0-0,2-0,1-0):4587:Stångebro Ishall

Is the Round:1 a typical input to be left out?
Could you say from those above, what would be: "Matches" "Wins" "Ties" "Losses"? Could you show an example of output, instead of definitions of what you would like?
Please, include in the example what would you like to sort by.
# 3  
Old 01-09-2016
Ok, let me explain a bit better.

I got a text file containing information like this

Code:
 
Brynäs IF:Djurgårdens IF:Färjestads BK:HV 71:Linköpings HC:Leksands IF:Luleå HF:MIF Redhawks:MODO Hockey:Södertälje SK:Timrå IK:Frölunda HC
Round:1
20020923:Leksands IF-Brynäs IF:2-3:(1-1,0-1,1-1):6298:Leksands Isstadion
20020924:Linköpings HC-Djurgårdens IF:3-0:(0-0,2-0,1-0):4587:Stångebro Ishall
20020924:MIF Redhawks-Frölunda HC:3-1:(2-1,1-0,0-0):4748:Malmö Isstadion
20020924:HV 71-Luleå HF:6-4:(2-1,3-0,1-3):6093:Kinnarps Arena
20020924:Timrå IK-MODO Hockey:4-1:(2-1,1-0,1-0):5366:Timrå Isstadion
20020924:Södertälje SK-Färjestads BK:2-3:(1-1,1-1,0-1):6426:Scaniarinken
Round:2
20020926:Färjestads BK-Timrå IK:1-3:(1-0,0-2,0-1):7142:Löfbergs Lila Arena
20020926:MODO Hockey-HV 71:1-2:(1-1,0-0,0-1):4057:Kempehallen
20020926:Luleå HF-MIF Redhawks:3-1:(0-1,2-0,1-0):5501:COOP Arena
20020926:Frölunda HC-Leksands IF:5-2:(3-1,1-1,1-0):11693:Scandinavium
20020926:Brynäs IF-Linköpings HC:5-2:(3-0,1-2,1-0):4195:Gavlerinken
20020926:Djurgårdens IF-Södertälje SK:2-1:(0-0,1-0,0-1,0-0,1-0):6749:Hovet,Johanneshov

It starts with every team in the league (just once). After that it's round 1.
Every round contains results from 6 games. Each match entry is organized into the following fields date: teams: matchscore: periodscores: spectators: arena. In total there is 50 rounds.

The calculation of match points should be as followed
if there is a victory for a team within three periods it gets 3 points.
If there is a loss for a team within three periods it gets 0 points.
If there is a tie after three periods each team get at least 1 point each. If a team wins within one of the two additional periods it gets 1 extra point.

I wanna do a script to do a summary of the matches and results.
I want my output to like something like this

Code:
Points    Team                  Matches  Wins    Ties   Losses
112        Djurgårdens If       50        33       9      8
97        Timrå If             50         30        5      15

And so on..

I'm thinking I need a loop to go over each team's home and away game. Then I need some kind of loop to see if a match contains more than 3 periods. Last I need a loop to read the score and give points.

I don't really know how to sort the text file to get just the information I need for each loop.

Any kind of help would be appriciated
# 4  
Old 01-09-2016
Not sure I understand those extra periods' evaluation.

And, why do you think sorting is essential?

Try this as a starting point:
Code:
awk -F: '
NR==1   {print "Points\tTeam\tMatches \tWins\tTies\tLosses"
         for (n = split ($0, TEAM); n > 0; n--) POINTS[TEAM[n]]=0
         next
        }

/^Round/        {next}

        {    split ($2, TM, "-")
             split ($3, RE, "-")
         P = split ($4, PR, ",")
         if (P > 3)             {POINTS[TM[1]]++
                                 POINTS[TM[2]]++
                                 TIES[TM[1]]++
                                 TIES[TM[2]]++
                                 VAL = 1
                                }
         else                    VAL = 3

         if (RE[1] > RE[2])     {POINTS[TM[1]] += VAL
                                 WINS[TM[1]]++
                                 LOSSES[TM[2]]++
                                }
         if (RE[1] < RE[2])     {POINTS[TM[2]] += VAL
                                 WINS[TM[2]]++
                                 LOSSES[TM[1]]++
                                }
        }
END     {for (p in POINTS) print POINTS[p], p, MATCHES[p], WINS[p]+0, TIES[p]+0, LOSSES[p]+0
        }
' OFS="\t" file
Points	Team	Matches 	Wins	Ties	Losses
3	Frölunda HC		1	0	1
3	MIF Redhawks		1	0	1
3	Färjestads BK		1	0	1
0	MODO Hockey		0	0	2
0	Leksands IF		0	0	2
2	Djurgårdens IF		1	1	1
1	Södertälje SK		0	1	2
6	Timrå IK		2	0	0
3	Luleå HF		1	0	1
6	Brynäs IF		2	0	0
6	HV 71		2	0	0
3	Linköpings HC		1	0	1

This User Gave Thanks to RudiC For This Post:
# 5  
Old 01-10-2016
Thanks for the help!

Got a few questions tho..

I added Matches counter to the script

Code:
awk -F: '
NR==1   {print "Points\tTeam       \tMatches\tWins\tTies\tLosses"
         for (n = split ($0, TEAM); n > 0; n--) POINTS[TEAM[n]]=0
         next
        }

/^Round/        {next}

        {    split ($2, TM, "-")
             split ($3, RE, "-")
         P = split ($4, PR, ",")
         if (P > 3)             {POINTS[TM[1]]++
                                 POINTS[TM[2]]++
                                 TIES[TM[1]]++
                                 TIES[TM[2]]++
                                 MATCHES[TM[1]]++
                                 MATCHES[TM[2]]++
                                 VAL = 1
                                }
         else                    VAL = 3

         if (RE[1] > RE[2])     {POINTS[TM[1]] += VAL
                                 WINS[TM[1]]++
                                 LOSSES[TM[2]]++
                                 MATCHES[TM[1]]++
                                 MATCHES[TM[2]]++
                                }
         if (RE[1] < RE[2])     {POINTS[TM[2]] += VAL
                                 WINS[TM[2]]++
                                 LOSSES[TM[1]]++
                                 MATCHES[TM[1]]++
                                 MATCHES[TM[2]]++
                                }
        }
END     {for (p in POINTS) print POINTS[p], p, MATCHES[p]+0, WINS[p]+0, TIES[p]+0, LOSSES[p]+0
        }
' OFS="\t" season2002-2003.txt

However the ouput gets like this

Code:
Points    Team           Matches    Wins    Ties    Losses
66    S�dert�lje SK    59    23    9    27
79    Lule� HF    62    25    12    25
79    HV 71    63    24    13    26
60    MIF Redhawks    60    22    10    28
53    Bryn�s IF    60    17    10    33
88    Djurg�rdens IF    57    29    7    21
42    Link�pings HC    59    13    9    37
68    Leksands IF    56    24    6    26
90    Timr� IK    58    30    8    20
94    F�rjestads BK    60    30    10    20
78    MODO Hockey    58    28    8    22
103    Fr�lunda HC    60    35    10    15

The count on how many matches each team play in the text file doesn't match. (should be 50, bcuz it's 50 rounds)
Can I do a counter, like count the word "round" and add that to Matches for each team?

Atm incase of a tie (after 3 periods), both team get 1 point each, I need a loop to give one of the teams 1 extra point if they scored in period 4 or 5 (which is the overtime).

---------- Post updated at 12:00 PM ---------- Previous update was at 11:07 AM ----------

I think I understand the problem with matches.
The script with periods more than 3 counts as a tie, and give each team +1 in matches.
After that the script search the result and add +1 in matches for each team.
However, if one of the team wins in overtime the count for matches is +1 for the tie and +1 for the result.

Example
Code:
20020926:Djurgårdens IF-Södertälje SK:2-1:(0-0,1-0,0-1,0-0,1-0):6749:Hovet,Johanneshov

How do I fix this so that incase of an overtime victory, the count for matches doesn't get +1 from tie and +1 from result?
# 6  
Old 01-11-2016
Yes, you're right, I forgot the matches' count although arranged for in the header and the array. Try
Code:
awk -F: '
NR==1   {print "Points\tTeam       \tMatches\tWins\tTies\tLosses"
         for (n = split ($0, TEAM); n > 0; n--) POINTS[TEAM[n]]=0
         next
        }

/^Round/        {next}

        {    split ($2, TM, "-")
             split ($3, RE, "-")
         P = split ($4, PR, ",")
         MATCHES[TM[1]]++
         MATCHES[TM[2]]++
         if (P > 3)             {POINTS[TM[1]]++
                                 POINTS[TM[2]]++
                                 TIES[TM[1]]++    
                                 TIES[TM[2]]++   
                                 VAL = 1
                                }
         else                    VAL = 3

         if (RE[1] > RE[2])     {POINTS[TM[1]] += VAL
                                 WINS[TM[1]]++
                                 LOSSES[TM[2]]++
                                }
         if (RE[1] < RE[2])     {POINTS[TM[2]] += VAL
                                 WINS[TM[2]]++
                                 LOSSES[TM[1]]++
                                }
        }
END     {for (p in POINTS) print POINTS[p], p, MATCHES[p]+0, WINS[p]+0, TIES[p]+0, LOSSES[p]+0
        }
' OFS="\t" season2002-2003.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in Sorting a file

Hi Unix Admins, I wanted to sort a file in a specific order, i.e the input file contains two fields and the first column is not unique and had to be sorted. example Input File ------- 2014-10-21:Rand1 2014-11-02:Rand2 2014-11-02:Rand3 2014-11-02:Rand4 2014-11-03:Rand5 2014-11-04:Rand6... (4 Replies)
Discussion started by: Naveenezone
4 Replies

2. Solaris

What about sorting a 5G file?

Hi Guys, My client (dear clients, I hate to love you) has the funky idea of sorting a 5G flat file. Certainly enough, this is taking forever and also fulls the / of our machine. Any idea of how we could proceed to make this a little bit more efficient? Maybe by forcing sort to "stay in... (7 Replies)
Discussion started by: plmachiavel
7 Replies

3. UNIX for Dummies Questions & Answers

Sorting data in file based on field in another file

Hi, I have two files, one of which I would like to sort based on the order of the data in the second. I would like to do this using a simple unix statement. My two files as follows: File 1: 12345 1 2 2 2 0 0 12349 0 0 2 2 1 2 12350 1 2 1 2 2 2 . . . File2: 12350... (3 Replies)
Discussion started by: kasan0
3 Replies

4. UNIX for Dummies Questions & Answers

sorting s file

how would i sort a file on the third column based on numerical value instead of the ASCII order? (1 Reply)
Discussion started by: trob
1 Replies

5. Shell Programming and Scripting

Finding & Moving Oldest File by Parsing/Sorting Date Info in File Names

I'm trying to write a script that will look in an /exports folder for the oldest export file and move it to a /staging folder. "Oldest" in this case is actually determined by date information embedded in the file names themselves. Also, the script should only move a file from /exports to... (6 Replies)
Discussion started by: nikosey
6 Replies

6. Shell Programming and Scripting

Sorting file

I have the file as follow: A: 60 B: 80 C: 40 D: 11 E: 100 I want to sort the file and get the output to file as follow: E: 100 B: 80 A: 60 C: 40 D: 11 Could any one help me please? (1 Reply)
Discussion started by: moutaz1983
1 Replies

7. Shell Programming and Scripting

file sorting

i have a data in afile like this ************************************** sree sree@yahoo.com 98662323432 ************************************* phani phani@yahoo.com 98662323344 ************************************* i want to sort the file with respect to name. how can i do this. thank... (5 Replies)
Discussion started by: phani_sree
5 Replies

8. Programming

regarding file sorting

i ahve a file like: ************************************* sree 122132 12321 *********************************** phani 21321 3213214 ****************************** dddsds 213213123 23213213 ******************************* i want to sort the file with respect to name how we can do this... (1 Reply)
Discussion started by: phani_sree
1 Replies

9. Shell Programming and Scripting

sorting file

hi everyone, i have a document where i have email addresess and names, i need to check if the email addresses are uniq, if they repeat erase one of them, how can i do that? document sample: aD00763357@cucei.udg.mx,ABRAHAM ANTONIO SEVERIANO a199721111@cucei.udg.mx,ABRAHAM GONZALEZ... (4 Replies)
Discussion started by: sx3v1l_1n51de
4 Replies

10. Shell Programming and Scripting

Help sorting file.

Hi, I have this file (filex) 07-11-2003 10:11:12!cccc!ddd!eeeeeeee 07-11-2003 09:11:11!dddd!kkkkk!xxxxxx 09-12-2003 14:18:43!aaaa!bbbbb!cccc where I need to sort it by date+time in this order: 09-12-2003 14:18:43!aaaa!bbbbb!cccc 07-11-2003 10:11:12!cccc!ddd!eeeeeeee 07-11-2003... (3 Replies)
Discussion started by: gio123bg
3 Replies
Login or Register to Ask a Question