Bash sort an unusual order. Problem with spaces?

A lot of time has passed with an error that tracks sorting ...

Can someone explain why I get this unsorted result when bash docs tell me that the separator is the transition from white to non-white characters? Should the first field be sorted?

>sort myfile.txt 10_10000000 19 10_10000001 20 10_10000002 19 10_10000003 17 10_10000004 16 10_1000000 44 10_10000005 16 10_10000006 16 10_10000007 17 10_10000008 16 

of course, using +0 -1 gives me the expected result:

 >sort +0 -1 myfile.txt 10_1000000 44 10_10000000 19 10_10000001 20 10_10000002 19 10_10000003 17 10_10000004 16 10_10000005 16 10_10000006 16 10_10000007 17 10_10000008 16 

Some metainfo:

 >type sort sort is hashed (/bin/sort) 

I use

 sort (GNU coreutils) 5.97 >locale LANG=en_US.UTF-8 LC_CTYPE="en_US.UTF-8" LC_NUMERIC="en_US.UTF-8" LC_TIME="en_US.UTF-8" LC_COLLATE="en_US.UTF-8" LC_MONETARY="en_US.UTF-8" LC_MESSAGES="en_US.UTF-8" LC_PAPER="en_US.UTF-8" LC_NAME="en_US.UTF-8" LC_ADDRESS="en_US.UTF-8" LC_TELEPHONE="en_US.UTF-8" LC_MEASUREMENT="en_US.UTF-8" LC_IDENTIFICATION="en_US.UTF-8" LC_ALL= 
+6
sorting bash
source share
2 answers

I think you see language-based problems. Some (many?) Locales affect the sorting method, as some characters are ignored. In this case, it seems that the space between the fields is ignored when you do not specify the fields to sort. Delete the space and you will see that the line that looks like in the wrong place is correct.

If you start sorting with a different language, you will probably get a different result:

 $ LANG=C sort myfile.txt 

My default locale is en_AU.UTF-8, and I see the original sort results. When I set LANG = C, I see the expected results.

+9
source share

Works for me correctly:

 $ sort myfile.txt 10_1000000 44 10_10000000 19 10_10000001 20 10_10000002 19 10_10000003 17 10_10000004 16 10_10000005 16 10_10000006 16 10_10000007 17 10_10000008 16 $ sort --version sort (GNU coreutils) 8.5 

Perhaps your version requires the -n flag to include a sort number?

+2
source share

All Articles