The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Changing root group to group from other mjkroner SUN Solaris 4 03-05-2009 04:25 AM
retrieving all group names with a given group number Andrewkl UNIX for Advanced & Expert Users 3 10-25-2008 04:07 AM
Monkcast #12: IBM HW group OEMs Solaris to chagrin of SW group & a ... - ZDNet.com bl iBot UNIX and Linux RSS News 0 08-17-2007 04:30 PM
entry in /etc/group too long - problem using sudo with %group poli SUN Solaris 4 12-21-2004 09:50 AM
NIS group vs local group vjsony UNIX for Dummies Questions & Answers 3 05-19-2003 09:54 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-15-2008
RacerX's Avatar
RacerX RacerX is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 38
SOLVED:Sort cmd to get GROUP BY?

Is there any way using the sort command to get something like a GROUP BY clause?

I'm trying to sort through race_event records and group them by those individuals that raced together in the same race on the same date at the same track, but i run into a problem when there were days when the track had qualifying races, plus regular races on the same day; so there could be two Race 1's, Race 2's, etc for the same date at the same track.
I tried a sort like:
Code:
sort -t':' -k3 -k4 -k9 -k29 -k8 -k28 ~/Desktop/1998/AllRaceEvents98.txt > ~/Desktop/sortedevents.txt
where
k3 is a date
k4 is the racetrack
k9 is the class of race (would show it was a qualifier or a regular race)
k29 is the race number
k8 is the purse of race (would show 0 for qualifier and dollar amount for regular race)
k28 is the names of the top three finishers

but this does not sort right: it does not group them together into their respective races correctly. Any suggestions?

Last edited by RacerX; 10-16-2008 at 07:31 AM.. Reason: solved
  #2 (permalink)  
Old 10-15-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink I think you need to experiment with full key definitions

Without seeing the data, I can only simulate. Perhaps the following example will help illustrate:

sample file
Code:
> cat file99
joe:1:alpha:4:5
jim:4:beta:16:9
bette:2:comma:99:0
dennis:3:per:12:12
harry:2:comma:12:13
olivia:1:beta:4:4
Trying to sort based on key field 3 then field 2. Field 3 should put the two beta people together and then the second field should put olivia before jim based on the 1 and 4 in their records. This did not happen.
Code:
> sort -t: -k3 -k2 file99
joe:1:alpha:4:5
jim:4:beta:16:9
olivia:1:beta:4:4
harry:2:comma:12:13
bette:2:comma:99:0
dennis:3:per:12:12
I then tried by putting explicit field definitions (start and end locations). So I am sorting on the first 3 characters of field 3 to get the beta people together. Then sort on field 2 - getting olivia before jim.
Code:
> sort -t: -k3.1,3.3 -k2 file99
joe:1:alpha:4:5
olivia:1:beta:4:4
jim:4:beta:16:9
harry:2:comma:12:13
bette:2:comma:99:0
dennis:3:per:12:12
  #3 (permalink)  
Old 10-15-2008
RacerX's Avatar
RacerX RacerX is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 38
i tried doing a sort using explicit definition as suggested on a couple of fields but again i could not get it to come out right.

I think i don't understand enough about how sort actually works: when i specify certain fields to sort on, what does it do sorting-wise with the fields i have not specified?

I'm beginning to think it's also sorting on a field i have not specified and that is why it is messing up the grouping.
  #4 (permalink)  
Old 10-15-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,867
Could you post sample input and example of the desired output?
  #5 (permalink)  
Old 10-15-2008
RacerX's Avatar
RacerX RacerX is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 38
Code:
Data records deleted
I'd like to get all the individuals that raced in the same race, on the same date at the same track together, but you'll notice after i do the sort in the instance above two of the individuals (bolded) ended up getting grouped with another race down on lines 616120 and 616121 when they should have followed the other individuals in their race at lines 616072 and 616073.

I hope that makes some sort of sense....

Last edited by RacerX; 10-16-2008 at 07:41 AM.. Reason: solved
  #6 (permalink)  
Old 10-15-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink A couple of things to think about

Sort is normally alphabetic - thus a field with a 12 will be before a 2 or 3. You will need the 'n' option on the field to make numeric.

To help determine where your script has gone astray, I would suggest you verify that your logic is fine for just one key. Then, add a second key; and so on until you discover the problem.

And from more investigation, I do not see any records improperly sorted.

Code:
> sort file200 -k3n -k4.1,4.3 -k9.1,9.10 -k29n -k8.1,8.2 -k28.1,28.10 | cut -d":" -f2-4,8-9,28-29

{data records here --- removed once issue solved --- not part of issue}

>

Last edited by joeyg; 10-16-2008 at 08:39 AM.. Reason: Added sample output from a test
  #7 (permalink)  
Old 10-16-2008
RacerX's Avatar
RacerX RacerX is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 38
Quote:
Originally Posted by joeyg View Post
Sort is normally alphabetic - thus a field with a 12 will be before a 2 or 3. You will need the 'n' option on the field to make numeric.
Thanks for this explanation, i think the 'n' option was the key to fixing my sorting problem.

Quote:
Originally Posted by joeyg View Post
And from more investigation, I do not see any records improperly sorted.
Sorry i think my ". . ." between the records did not properly indicate that there were 49 other records between the two sets of records that actually needed to be together.

Anyhow, thanks for all your help, you steered me to a solution.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:23 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0