Weird "sort" behavior


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Weird "sort" behavior
# 1  
Old 11-09-2011
Weird "sort" behavior

Hi,

I'm trying to sort a text file "test":
Code:
S12
S_S12
S_S1_12
S15
S_N15
S_N1_15

By "sort test", I get:
Code:
S12
S15
S_N1_15
S_N15
S_S1_12
S_S12

It seems weird:
Comparing Line 2 and Line 3, it must be that '-' is bigger than '1'; however, comparing Line 3 and Line 4, it seems that '5' is bigger than '-'.

And the result of comparing one line to a previous line using strcmp function is:
Code:
S12
S15                        3
S_N1_15                46
S_N15                    -42
S_S1_12                 5
S_S12                     -45.

It seems that "S_N15" and "S_S12" are not in the correct position.

The result (according to strcmp) should be:
Code:
S12
S15
S_N15
S_N1_15
S_S12
S_S1_12


Last edited by vgersh99; 11-09-2011 at 07:27 PM.. Reason: code tags, please!
# 2  
Old 11-09-2011
First of all you have underscores in your strings and not dashes.
By default 'sort' sort alphabetically based on your local.

The octal ASCII value of the underscore is 137
The octal ASCII value of the 1 is 061
And upper case letters start at 101

This is what I get based on your sample after sorting with not switches:
Code:
S12
S15
S_N15
S_N1_15
S_S12
S_S1_12

which looks correct based on the previous ASCII value explanation.
I'm in the US. What local are you at?

Or better yet, how would you like to sort?
# 3  
Old 11-09-2011
The kernel I'm using is:
Linux ccm18 2.6.32-31-generic #61-Ubuntu SMP Fri Apr 8 18:25:51 UTC 2011 x86_64 GNU/Linux

Can't figure out why the "sort" behaves differently from yours.

Quote:
Originally Posted by vgersh99
First of all you have underscores in your strings and not dashes.
By default 'sort' sort alphabetically based on your local.

The octal ASCII value of the underscore is 137
The octal ASCII value of the 1 is 061
And upper case letters start at 101

This is what I get based on your sample after sorting with not switches:
Code:
S12
S15
S_N15
S_N1_15
S_S12
S_S1_12

which looks correct based on the previous ASCII value explanation.
I'm in the US. What local are you at?

Or better yet, how would you like to sort?
# 4  
Old 11-12-2011
Quote:
Originally Posted by intermilan
The kernel I'm using is:
Linux ccm18 2.6.32-31-generic #61-Ubuntu SMP Fri Apr 8 18:25:51 UTC 2011 x86_64 GNU/Linux

Can't figure out why the "sort" behaves differently from yours.
Most likely your locale isnt "C" so use the locale command to find out what it is set to...
Code:
locale

Output of above command lists a bunch of LC_* variables and if LC_COLLATE isnt set to "C" then do so or just set LC_ALL to "C" which overrides all settings and sets all LC_* variables to "C"...
Code:
export LC_COLLATE="C"
or
export LC_ALL="C"

Now if you rerun the sort you will get the desired result...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract delta records using with "comm" and "sort" commands combination

Hi All, I have 2 pipe delimited files viz., file_old and file_new. I'm trying to compare these 2 files, and extract all the different rows between them into a new_file. comm -3 < sort file_old < sort file_new > new_file I am getting the below error: -ksh: sort: cannot open But if I do... (7 Replies)
Discussion started by: njny
7 Replies

2. Shell Programming and Scripting

Weird behavior of command "local"

Hi there, I'm running into a very weird situation. Let's forget about the purpose of my initial script please. I noticed the bug whatever I'm trying to do. I'm on an old server running bash 3.1.17. Say we have the following script : foo:~# cat /tmp/test #!/bin/bash f1() { local... (9 Replies)
Discussion started by: chebarbudo
9 Replies

3. Shell Programming and Scripting

Commenting out "expr" creates weird behavior

This really puzzles me. The following code gives me the error 'expr: syntax error' when I try to do multi-line comment using here document <<EOF echo "Sum is: `expr $1 + $2`" EOF Even if I explicitly comment out the line containing the expr using "#", the error message would still exist... (3 Replies)
Discussion started by: royalibrahim
3 Replies

4. UNIX for Advanced & Expert Users

"╭─ " Character combo in $PATH causes strange autocompletion behavior in zsh

I've posted about this before, but only recently narrowed the problem down to a specific cause. Ok, first of all, the behavior: It occurs when autocompletion brings up its list (not when there is only a single option). Basically, if I were to type, say, cd ~/<TAB> I would get something... (2 Replies)
Discussion started by: marshaul
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Meaning of "b" modifier in "sort" command

I need to sort the following file by the rhdiskpower devices in the last column: Total_MB Free_MB OS_MB Name Failgroup Library Label UDID Product Redund Path 1024 851 1024 OCRVOT1_0000 OCRVOT1_0000 System UNKNOWN ... (3 Replies)
Discussion started by: wjssj
3 Replies

7. UNIX for Dummies Questions & Answers

weird password popup on "admin" mentions

I had a site that sold stock photography and some guys I know set up a shopping cart and gallery system for it. One thing they did was give me an admin page (mysite.net/admin) where I could upload new images and change prices and all that. For that admin page they set a password popup prompt. I... (4 Replies)
Discussion started by: lex0062
4 Replies

8. Shell Programming and Scripting

"Odd" behavior exiting shell script

Is it normal behavior for a shell script that terminates to terminate its parent shell when executed with the "." option? For example, if I have the example script (we'll name it ex.sh): #!/bin/sh if then echo "Bye." exit 2 fi And I execute it like this: >./ex.sh It... (6 Replies)
Discussion started by: DreamWarrior
6 Replies

9. OS X (Apple)

Weird "security" bahavior with SSL certificates

Hello, I have been attempting to automate the addition of SSL certificates to keychains on a MAC using the "security" command. I've noticed two things, 1 of which I don't understand. 1. If I use something like "security add-trusted-cert -d -k /System/Library/Keychains/SystemRootCertificates... (1 Reply)
Discussion started by: prafulnama
1 Replies

10. Shell Programming and Scripting

Weird problem with output from "date '+3600*%H+60*%M+%S' "

Hi, I came across a script a few months ago that allowed you to use the following script to include the current time into your prompt (useful from auditting purposes): # Set Prompt typeset -RZ2 _x1 _x2 _x3 let SECONDS=$(date '+3600*%H+60*%M+%S')... (5 Replies)
Discussion started by: m223464
5 Replies
Login or Register to Ask a Question