Sort command - strange behaviour


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Sort command - strange behaviour
# 8  
Old 05-28-2008
Using option g?

Code:
sort -t";" -k 2,2rg file.to.sort

# 9  
Old 05-29-2008
@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.
# 10  
Old 05-29-2008
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.
# 11  
Old 05-29-2008
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.
# 12  
Old 05-29-2008
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 Smilie
# 13  
Old 05-29-2008
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
# 14  
Old 05-29-2008
Nice solution! Smilie

Edit:
Just saw that sort does not have that switch on AIX (didn't look for a similar one) but on Linux, nice though Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

linux sort command produces strange output

cat a .a ba .b bb .c bc sort a .a .b ba bb bc .c NOTE: .a and .b appears before ba and bb, where as .c appears after bc. In general (3 Replies)
Discussion started by: ajb
3 Replies

2. Shell Programming and Scripting

Strange "cut" command's behaviour

Hi, Suppose if I have a file having data like this: $ cat file.txt A B C D And, if I do a cut operation like this: $ cut -d" " -f2 file.txt The output is A C D This is the same for even if we try to get the field 3 with -f3 (assume line 2 has 3 fields : C E F). The above... (1 Reply)
Discussion started by: royalibrahim
1 Replies

3. Red Hat

Crontab strange behaviour

Hi all, I'm having this scenario which for the moment I cannot resolve. :( I wrote a script to make a dump/export of the oracle database. and then put this entry on crontab to be executed daily for example. The script is like below: cat /home/oracle/scripts/db_backup.sh #!/bin/ksh ... (3 Replies)
Discussion started by: enux
3 Replies

4. Shell Programming and Scripting

strange behaviour from sed???

Hi all, I want to do a very simple thing with sed. I want to print out the line number of a disk I have defined in /etc/exports, so I do: It's all good, but here's the problem. When I define md0 in a variable, I get nothing from sed: Why is that? can anybody please help? Thanks (2 Replies)
Discussion started by: alirezan
2 Replies

5. UNIX for Dummies Questions & Answers

Strange Program behaviour

Had a strange thing going on with my code. It's ok I figured it out for myself.... (2 Replies)
Discussion started by: mrpugster
2 Replies

6. UNIX for Advanced & Expert Users

Strange sed behaviour

$ echo a.bc | sed -e "s/\|/\\|/g" |a|.|b|c| $ Is the behavior of the sed statement expected ? Or is this a bug in sed ? OS details Linux 2.6.9-55.0.0.0.2.ELsmp #1 SMP Wed May 2 14:59:56 PDT 2007 i686 i686 i386 GNU/Linux (8 Replies)
Discussion started by: vino
8 Replies

7. Shell Programming and Scripting

A Strange Behaviour!!!

Can some-one give me a view to this : I have a directory in an unix server, having permissions r-xr-xr-x .This directory is basically a source directory. Now there is another directory basically the destination directory which has all the permissions. Note:I log in as not the owner,but user... (5 Replies)
Discussion started by: navojit dutta
5 Replies

8. UNIX for Dummies Questions & Answers

a strange message when executing the sort command

Dear all, when I issue the command: gunzip -c file.gz |sort the command is executed normally and correctly but a message keeps appearing everytime I run the command: the message: sort: missing NEWLINE added at end of input file STDIN Does anyone know what is the meaning of this message?... (3 Replies)
Discussion started by: marwan
3 Replies

9. Shell Programming and Scripting

Help me to resolve uncertian behaviour of a sort command

I have got a file BeforeSort.txt having 40 fields seperated by "|" First field= RecordType (Value will be P or FP) Second field= CamCode Third field = UpdatingDate Fourth field = ProductType Fifth field = ActionCode (Value may be 01, 02 or 03) Sixth field = ProductCode and so on My... (1 Reply)
Discussion started by: pankajrai
1 Replies

10. Linux

/etc/passwd strange behaviour!

Hi there, first of all, here is my conf of a uname -a Linux SAMBA 2.4.18-4GB #1 Wed Mar 27 13:57:05 UTC 2002 i686 unknown on a fedora machine. Here is my problem: every once in a while, the line containing root disappears in the /etc/passwd, disabling all logging on my server. Any one have... (0 Replies)
Discussion started by: penguin-friend
0 Replies
Login or Register to Ask a Question