The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Strange sed behaviour vino UNIX for Advanced & Expert Users 8 02-12-2008 03:51 AM
A Strange Behaviour!!! navojit dutta Shell Programming and Scripting 5 12-21-2007 01:35 AM
a strange message when executing the sort command marwan UNIX for Dummies Questions & Answers 3 04-27-2007 04:32 AM
Help me to resolve uncertian behaviour of a sort command pankajrai Shell Programming and Scripting 1 12-21-2005 11:12 AM
/etc/passwd strange behaviour! penguin-friend Linux 0 06-06-2005 09:00 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #8  
Old 05-28-2008
Registered User
 

Join Date: Oct 2006
Location: Belgium
Posts: 171
Using option g?

Code:
sort -t";" -k 2,2rg file.to.sort
Reply With Quote
Forum Sponsor
  #9  
Old 05-28-2008
Moderator
 

Join Date: Sep 2007
Location: Germany
Posts: 1,031
@ripat
Nope doesn't work - check the second column when having the ":" as delimeter. It only sorts the 1st number in front of the 1st ":".

@miwinter
Hm, I tested it with mawk on Debian Linux and on AIX with awk and no duplicate output. Not sure what you have to add to supress the duplicates... if you find no option to tell it awk, maybe pipe it into "uniq" at the end.
Reply With Quote
  #10  
Old 05-28-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Or split the input to make it easier for sort to parse the fields you want.

Code:
vnix$*awk -F';' '{ split($2,t,":"); print t[1] ";" t[2] ";" t[3] ";" $0 }' <<HERE |
> GleamMIPostCanadaExtractJob;9196:53:12
> GleamMIAGREERepAllBackOutJob;9025:39:12
> GleamMIAGREEProdFacilCombJob;9025:29:36
> GleamMIAGREEExcRateHistExtractJob;9025:21:26
> GleamMIAGREEDynamicParamJob;9025:19:10
> GleamMIAGREEClassPODLoadJob;9025:09:43
> GleamMIAGREEClassExtractJob;9025:11:35
> GLMLRP_Diff_HighlighterJob;989:08:56
> GLMLRP_ComparisonJob;989:13:42
> AD046;988:44:15
> HERE
> sort -t ';' -rn | cut -d ';' -f4-
GleamMIPostCanadaExtractJob;9196:53:12
GleamMIAGREERepAllBackOutJob;9025:39:12
GleamMIAGREEProdFacilCombJob;9025:29:36
GleamMIAGREEExcRateHistExtractJob;9025:21:26
GleamMIAGREEDynamicParamJob;9025:19:10
GleamMIAGREEClassExtractJob;9025:11:35
GleamMIAGREEClassPODLoadJob;9025:09:43
GLMLRP_ComparisonJob;989:13:42
GLMLRP_Diff_HighlighterJob;989:08:56
AD046;988:44:15
Take away the final cut to see what it's really doing.
Reply With Quote
  #11  
Old 05-29-2008
Registered User
 

Join Date: Oct 2006
Location: Belgium
Posts: 171
sort has a strange behavior. When you pipe him the second field using awk, the sort is ok:
Code:
$ awk -F";" '{print $2}' f | sort -rn
9196:53:12
9025:39:12
9025:29:36
9025:21:26
9025:19:10
9025:11:35
9025:09:43
989:13:42
989:08:56
988:44:15
But when you ask sort to do the very same i.e to split on the ; and sort on the second key, the output is, indeed, not as expected:
Code:
$ sort -t";" -k2rn f
GleamMIPostCanadaExtractJob;9196:53:12
GleamMIAGREEClassExtractJob;9025:11:35
GleamMIAGREEClassPODLoadJob;9025:09:43
GleamMIAGREEDynamicParamJob;9025:19:10
GleamMIAGREEExcRateHistExtractJob;9025:21:26
GleamMIAGREEProdFacilCombJob;9025:29:36
GleamMIAGREERepAllBackOutJob;9025:39:12
GLMLRP_ComparisonJob;989:13:42
GLMLRP_Diff_HighlighterJob;989:08:56
AD046;988:44:15
Very strange.
Reply With Quote
  #12  
Old 05-29-2008
Moderator
 

Join Date: Sep 2007
Location: Germany
Posts: 1,031
And one more...
Forgot about that you can use more than one Field Separator - so here a more compact version (ok, some cheating on the delimiters with sed in the end):

Code:
awk -F ";|:" 'BEGIN{OFS=":"} {print $1,$2,$3,$4}' infile| sort -t ":" -rn -k 2,2 -k 3,3 -k 4,4 | sed -n 's/\:/\;/p'
@era
Btw, funny awards on your site
Reply With Quote
  #13  
Old 05-29-2008
Registered User
 

Join Date: Oct 2006
Location: Belgium
Posts: 171
Got it! I had to read all possible options, even the one that are not well documented!

Code:
$ sort -t";" -s -k2,2rn f
GleamMIPostCanadaExtractJob;9196:53:12
GleamMIAGREERepAllBackOutJob;9025:39:12
GleamMIAGREEProdFacilCombJob;9025:29:36
GleamMIAGREEExcRateHistExtractJob;9025:21:26
GleamMIAGREEDynamicParamJob;9025:19:10
GleamMIAGREEClassExtractJob;9025:11:35
GleamMIAGREEClassPODLoadJob;9025:09:43
GLMLRP_ComparisonJob;989:13:42
GLMLRP_Diff_HighlighterJob;989:08:56
AD046;988:44:15
Option -s did the trick! Found solution by reading this man page:
Code:
   Finally, as a last resort when all keys compare equal (or if no
   ordering options were specified at all), `sort' compares the entire
   lines.  The last resort comparison honors the `-r' global option.  The
   `-s' (stable) option disables this last-resort comparison so that lines
   in which all fields compare equal are left in their original relative
   order.  If no fields or global options are specified, `-s' has no
   effect.
sort Linux Commands: Sort text files
Reply With Quote
  #14  
Old 05-29-2008
Moderator
 

Join Date: Sep 2007
Location: Germany
Posts: 1,031
Nice solution!

Edit:
Just saw that sort does not have that switch on AIX (didn't look for a similar one) but on Linux, nice though
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0