UNIX sort ignores whitespaces

Viewed 14406

Given a file txt:

ab
a c
a a

When calling sort txt, I obtain:

a a
ab
a c

In other words, it is not proper sorting, it kind of deletes/ignores the whitespaces! I expected this to be the behavior of sort -i but it happens with or without the -i flag.

I would like to obtain "correct" sorting:

a a
a c
ab

How should I do that?

7 Answers

You could use the 'env' program to temporarily change your LC_COLLATE for the duration of the sort; e.g.

/usr/bin/env LC_COLLATE=POSIX /bin/sort file1 file2

It's a little cumbersome on the command line but if you're using it in a script should be transparent.

Related