Re: sort - specifying sort fields/keys.

cga2000 <[email protected]>
Newsgroups gmane.comp.gnu.utils
Message-ID <[email protected]>
On Tue, Apr 08, 2008 at 11:22:27AM EDT, Bob Proulx wrote:
> cga2000 wrote:
> > I wanted to sort the output of the ls command by filename and with 
> > directories first and regular files last.
> > 
> > What I came up with does not quite behave as I expected:
> > 
> > $ ls -al --color=never | sort -k1.1,1.1r -k8f 
> 
> You shouldn't ever need --color=never there unless you have aliased ls
> with ls --color=always.  You don't want ls --color=always (really you
> don't) and therefore you won't need to undo it with --color=never.

You're absolutely right. 

In fact I didn't want to have to worry about the effects of color escape
codes on sort field counting at that point and that's why I initially
specified --color=never.

On my system "ls" is aliased to "ls --color=auto".

Regrettably the man page provided on my system lists the three options
(never, always, auto) but does not give any explanation as to what they
actually do.

I did a 

$ ls -al | sort -k1.1,1.1r -k8f

and the output is identical (properly sorted with dots ignored)

OTOH, I have the "l" shortcut aliased to "ls -alh --full-time
--color=always" and I verified that

$ l | sort -k1.1,1.1r -k8f

screws up big time .. the output is "sort of" sorted .. 

Does --color=auto mean add colors when writing to a terminal and don't
add them when writing to a file (pipe etc.)..?

> > Why does .efile appear together with my other two [Ee]file's rather than
> > just below .clewn_keys?
> > 
> > Looks like sort is ignoring the dots .. ??
> 
> You probably have set a locale setting that ignores punctuation.

OK. This is beginning to make "sense". 

> > Side-effect of the "f" flag specified with my second sort key ..?
> 
> No.
> 
> > Something to do with my locale (en_US)?
> 
> Yes.  If a locale is set then the collation sequence for the locale is
> used.  This is controlled outside of sort (and also ls and bash and
> anything else that produces sorted output) and must be respected if
> set.
> 
>   http://www.gnu.org/software/coreutils/faq/#Sort-does-not-sort-in-normal-order_0021

Thanks.  Good doc.

> Apparently the people who defined the collating sequence for the en_*
> locales confused working with data on a computer with working with
> text on a computer.  The locale collating sequences for en_* ignores
> punctuation and folds case by default!

Given the symptoms and the nature of sort I would probably never have
figured that out myself. That there may be circumstances where this
comes in handy I do not doubt .. But as to making it the default for one
of (if not the) most widely-used locales?

> I have set the following in my environment to restore a standard sort
> ordering:
> 
>   export LANG=en_US.UTF-8 export LC_COLLATE=C

I gave  up on UTF-8 because I use mostly ELinks for browsing and afaik
it's not UTF-8 ready.

I tested with LC_ALL=POSIX (as recommended in your document) and the
"." was still being ignored.

So I issued the above export commands and (magically) data was sorted
as data .. 

Thank you very much for your clarification.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.