You are not logged in.
Pages: 1
When sorting with the `sort` coreutil, why is underscore (`_`) placed between A-Z letters rather than before or after?
$ echo "$(echo {A..Z}) _ " | sed 's/ /=true\n/g' | sort
A=true
B=true
C=true
D=true
...
R=true
S=true
_=true
T=true
U=true
V=true
W=true
X=true
Y=true
Z=true
$ $ printenv | sort
...
USER=random_sort
_=/usr/bin/printenv
VISUAL=vim
...Last edited by dahy (2017-03-22 22:09:10)
Offline
It isn't 'placed' anywhere, it's ignored (along with the = and the /), for more info read this...
https://www.gnu.org/software/coreutils/ … order_0021
Last edited by Slithery (2017-03-22 22:14:17)
Offline
I don't have the answer, but try echo "$(echo {A..Z}) _ " | sed 's/ /=true\n/g' | LC_ALL=c sort
O_o
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way
Offline
It is most often due to having LANG set to ‘en_US.UTF-8’ set in your user environment. At that point sort appears broken because case is folded and punctuation is ignored because ‘en_US.UTF-8’ specifies this behavior.
I have `LC_ALL` set to `en_US.UTF-8`.
$ printenv | ag ^LC_ | sort
LC_ADDRESS=en_US.UTF-8
LC_ALL=en_US.UTF-8
LC_COLLATE=en_US.UTF-8
LC_CTYPE=en_US.UTF-8
LC_IDENTIFICATION=en_US.UTF-8
LC_MEASUREMENT=en_US.UTF-8
LC_MESSAGES=en_US.UTF-8
LC_MONETARY=en_US.UTF-8
LC_NAME=en_US.UTF-8
LC_NUMERIC=en_US.UTF-8
LC_PAPER=en_US.UTF-8
LC_TELEPHONE=en_US.UTF-8
LC_TIME=en_US.UTF-8
$ Using `vim` instead will allow me to keep those settings.
printenv | vim -c 'sort u' -Offline
this confuses me, as slithery points out, the characters are ignored, but then why are ignored characters placed squarely between S and T? eg.
[ tye@t: ~ ]$ echo "$(echo {A..Z}) _ / = " | sed 's/ /=true\n/g' | sort
A=true
...
S=true
==true
_=true
/=true
T=true
...
Z=truethis is the same behaviour as in the OP, so surely there is some sorting going on here, correct?
Last edited by HiImTye (2017-03-23 05:27:39)
Offline
The relevant flag is LC_COLLATE and (at least in latin glyph using regions), you'll have a *much* simpler life if you set that to "C" in /etc/locale.conf
Why sort acts apparently "random" when sorting in UTF-8, I don't know either. Might be a bug, might just be utf-8 hackishnes *shrug*
Offline
but then why are ignored characters placed squarely between S and T?
I think that is due to the `true` that follows. If you put `false`, it will be placed between E and F.
I found another peculiarity in the sorting, though: Depending on the locale, lowercase letters come before or after uppercase letters. For instance, en_CA has AaBbCc... (whereas en_US, en_GB, de_CH, de_DE have aAbBcC...).
But depending on how the letters are passed to `sort`, en_CA may sort it like the other locales - compare the output of those two:
for i in {A..Z} {a..z}; do echo $i; done | LANG=en_CA.utf8 sort # AaBbCc...echo {A..Z} {a..z} | sed 's/ /\n/g' | lang=en_CA.utf8 sort # aAbBcC...I have absolutely no idea why this is so. When piping both into `xxd` instead of `sort`, the output is exactly the same.
I'm sorry, typo - it should've been `LANG=`, not `lang=`. Everything's fine.
Last edited by ayekat (2017-03-23 09:13:27)
Offline
HiImTye wrote:but then why are ignored characters placed squarely between S and T?
I think that is due to the `true` that follows. If you put `false`, it will be placed between E and F.
that's exacty right, changing the t to an f in true produces the behaviour you pointed out. faulty assumptions ![]()
Offline
It sounds like the problem has been identified and a good solution proposed. I couldn't replicate the issue for two reasons: 1) I already had LC_COLLATE="C" - doesn't everybody?? and 2) I couldn't bear to use a nested echo and subshell for no reason ![]()
echo {A..Z} "_ " | sed 's/ /=true\n/g' | sortYou could also do without the sed if you can live with one blank line:
echo -e "\n"{A..Z}"=true" "\n_=true" | sort"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
You could also do without the sed if you can live with one blank line:
the sed version also produces one blank line ![]()
Offline
Pages: 1