LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2011, 09:53 PM   #1
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
[bash]sort command - how to handle a tie condition


hi, i am using this code to create a sorted list by rating like so:
Code:
sort car-rank.tmp -n -r -t \" -k 12 | grep -n . > car-rank.txt
...
548:schneidz:  <worldCar carId="4390297" make="BMW" carName="Z4 M COUPE">    <physicsProfile acceleration="382" handling="414" rating="406" topSpeed="423" tier="2"/>
549:chun-li:  <worldCar carId="4554094" make="AUDI" carName="S5">    <physicsProfile acceleration="405" handling="380" rating="405" topSpeed="430" tier="2"/>
550:akuma:  <worldCar carId="7385671" make="BMW" carName="Z4 M COUPE">    <physicsProfile acceleration="387" handling="414" rating="403" topSpeed="410" tier="2"/>
551:ryu:  <worldCar carId="3638203" make="MAZDA" carName="RX-7">    <physicsProfile acceleration="424" handling="382" rating="402" topSpeed="402" tier="2"/>
552:ken:  <worldCar carId="6906067" make="LEXUS" carName="ISF">    <physicsProfile acceleration="417" handling="426" rating="402" topSpeed="364" tier="2"/>
553:sakura:  <worldCar carId="1487718" make="MAZDA" carName="RX-7">    <physicsProfile acceleration="415" handling="378" rating="401" topSpeed="411" tier="2"/>
...
but i would like the output to look more like:
Code:
...
548:schneidz:  <worldCar carId="4390297" make="BMW" carName="Z4 M COUPE">    <physicsProfile acceleration="382" handling="414" rating="406" topSpeed="423" tier="2"/>
549:chun-li:  <worldCar carId="4554094" make="AUDI" carName="S5">    <physicsProfile acceleration="405" handling="380" rating="405" topSpeed="430" tier="2"/>
550:akuma:  <worldCar carId="7385671" make="BMW" carName="Z4 M COUPE">    <physicsProfile acceleration="387" handling="414" rating="403" topSpeed="410" tier="2"/>
551:ryu:  <worldCar carId="3638203" make="MAZDA" carName="RX-7">    <physicsProfile acceleration="424" handling="382" rating="402" topSpeed="402" tier="2"/>
551:ken:  <worldCar carId="6906067" make="LEXUS" carName="ISF">    <physicsProfile acceleration="417" handling="426" rating="402" topSpeed="364" tier="2"/>
552:sakura:  <worldCar carId="1487718" make="MAZDA" carName="RX-7">    <physicsProfile acceleration="415" handling="378" rating="401" topSpeed="411" tier="2"/>
...
since ryu and ken are tied. does sort have some flag i can pass it that will rank things and be respectful of ties. am i forced to use awk for something like this (does anyone have an example) ?

thanks,
schneidz

Last edited by schneidz; 04-01-2011 at 09:59 PM.
 
Old 04-02-2011, 03:01 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
My guess is you would need to sort over multiple items so then a tie will use the second or more columns to influence the sort.
eg. Looking at your example you could use rating and topSpeed and then in the example given ken would be prior to ryu.
 
1 members found this post helpful.
Old 04-02-2011, 08:54 AM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
^ thx, i gave you a plus 1 but i am still open if anyone got any other ideas.

fyi: i changed all the user names except mine but i think the tie-breaker is alphabetically user name.

Last edited by schneidz; 04-02-2011 at 08:58 AM.
 
Old 04-02-2011, 10:06 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
hi, i h4x0rz-ed this solution:
Code:
[schneidz@hyper temp]$ cat rank.test | while read line; do  newrank=`echo $line | awk -F\" '{print $12}'`;  if [ $newrank -ne $oldrank ];  then   i=`expr $i + 1`;  fi;  echo $i - $line;  oldrank=$newrank; done
1 - schneidz: <worldCar carId="4390297" make="BMW" carName="Z4 M COUPE"> <physicsProfile acceleration="382" handling="414" rating="406" topSpeed="423" tier="2"/>
2 - chun-li: <worldCar carId="4554094" make="AUDI" carName="S5"> <physicsProfile acceleration="405" handling="380" rating="405" topSpeed="430" tier="2"/>
3 - akuma: <worldCar carId="7385671" make="BMW" carName="Z4 M COUPE"> <physicsProfile acceleration="387" handling="414" rating="403" topSpeed="410" tier="2"/>
4 - ryu: <worldCar carId="3638203" make="MAZDA" carName="RX-7"> <physicsProfile acceleration="424" handling="382" rating="402" topSpeed="402" tier="2"/>
4 - ken: <worldCar carId="6906067" make="LEXUS" carName="ISF"> <physicsProfile acceleration="417" handling="426" rating="402" topSpeed="364" tier="2"/>
5 - sakura: <worldCar carId="1487718" make="MAZDA" carName="RX-7"> <physicsProfile acceleration="415" handling="378" rating="401" topSpeed="411" tier="2"/>
thanks,
 
Old 04-03-2011, 04:56 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well here is an alternative to what you have and a single awk:
Code:
while read -r rank line; do (( rank != oldrank )) && (( i++ ));echo $i - $line;oldrank=$rank;done< <(awk -F\" '{print $12,$0}' rank.test)

awk -F\" 'oldrank != $12{i++}{print i,"-",$0;oldrank=$12}' rank.test
 
1 members found this post helpful.
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: Repeat commands until a condition is matched iniuria Programming 4 03-09-2011 09:54 AM
[SOLVED] Problem with BASH conditionals - passing condition as variable smaddox Programming 8 09-18-2009 11:21 AM
Two square brackets in bash condition tirengarfio Programming 1 07-07-2009 12:36 PM
multiple if condition in bash tikit Programming 6 11-12-2008 01:34 PM
Need help with bash script /simplified sort command/ szcs Linux - General 0 10-26-2008 11:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration