2

I'm running Ubuntu 20.0.4.

Consider these contents of the following file (suppose the file is named input) ...

/zzzzz foo bar
/hij zzz
xyz abc 
/abc 
ijk lmnop
qwer tyuiop 
abc def

If I run /bin/sort input, I get the following output ...

/abc 
abc def
/hij zzz
ijk lmnop
qwer tyuiop 
xyz abc 
/zzzzz foo bar

As you can see, the leading forward slash is totally ignored by the sort algorithm. There is nothing in the sort man page which says anything about forward slashes being treated in any kind of special way.

But if I replace each of the forward slashes with the $ character, this behavior does not occur. For example, suppose that a file which is named newinput contains these values ...

$zzzzz foo bar
$hij zzz
xyz abc 
$abc 
ijk lmnop
qwer tyuiop 
abc def

In other words, newinput is the same as input, except for the fact that / characters have been changed to $ characters. When I do /bin/sort newinput, I get these results ...

$abc 
$hij zzz
$zzzzz foo bar
abc def
ijk lmnop
qwer tyuiop 
xyz abc 

As you can see, in this case, the $ is indeed treated as a normal, sortable character by /bin/sort.

So, what's going on here? And how do I cause /bin/sort to pay attention to the / character?

Thank you in advance for any ideas and suggestions.

UPDATE: If I do this under Debian, the sort works properly, both with regard to / and $.

So, I'm wondering if this behavior might be a "feature" of Ubuntu.

2

1 Answer 1

0

Aha! I figured it out!

On my Ubuntu box, I have LC_ALL set to en_US.UTF8, and on my Debian box, I have LC_ALL set to C.UTF8. If I change LC_ALL to C.UTF8 on my Ubuntu box, the forward slash gets sorted as a regular character. And if I change LC_ALL to en_US.UTF8 on the Debian box, then the slashes get ignored there, also.

So this behavior appears to be an artifact of en_US.UTF8 encoding.

Is this correct behavior for en_US.UTF8?

Anyway, I now know how to work around this "feature" by an appropriate setting for LC_ALL.

PS: And also, as Stephen Harris pointed out above, setting LANG to C fixes this issue in the same way as setting LC_ALL to C.UTF8.

1
  • 1
    I'm a sysadmin in the USA, so when I think about sorting (actually "collation order"), I nearly always envision the order in the ASCII character set. To get results consistent with ASCII, I set LC_ALL=C.
    – Sotto Voce
    Sep 26, 2022 at 19:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .