Sort with case-sensitivity (lowercase before uppercase) in macOS in the Terminal
Question
I am trying to use the answer in GNU Sort by case-sensitive to sort a file in my Mac OS.
My file.txt
looks like this:
"Best"
"A"
"BEST"
In the second answer to the mentioned question, it says that one can do:
echo -e "d\nD\nc\nb\nB\na" | sort --ignore-case file.txt
and the output should be:
"A"
"Best"
"BEST"
However, when I do it in my computer, I get:
"A"
"BEST"
"Best"
It does not matter if I use the --ignore-case
or not, the output is the same.
Maybe LC_COLLATE=C
can not be set in Mac OS?
Does someone know how to use sort
and have lowercase < uppercase in Mac OS?
EDIT
@jaume recommended installing coreutils
and using the GNU sort. I tried brew install coreutils
and then gsort -f file.txt
but still had no luck.
La solution
Sort order is defined in your locale
.
locale
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL=
When LC_COLLATE is defined as LC_COLLATE="en_US.UTF-8" on macOS, LC_COLLATE uses the C or POSIX definiton. The default behavior of sort
is to sort in ascii order where uppercase has precedence over lowercase.
On Linux, when LC_COLLATE="en_US.UTF-8" the LC_COLLATE definiton is non-standard. The default behavior of sort
is to ignore-case and lowercase has precedence over uppercase.
To mimic Linux default sort
behavior on macOS, You can use tr
to "translate" lowercase letters to uppercase and uppercase to lowercase, sort with the ignore-case
option (-f
), then change the case back again.
tr 'a-zA-Z' 'A-Za-z' <file.txt | sort -f | tr 'a-zA-Z' 'A-Za-z'
EXAMPLE:
cat file.txt
"cat"
"Best"
"A"
"BEST"
"Castro"
"alfred"
"a"
"CAT"
tr 'a-zA-Z' 'A-Za-z' <file.txt | sort -f | tr 'a-zA-Z' 'A-Za-z'
"a"
"A"
"alfred"
"Best"
"BEST"
"Castro"
"cat"
"CAT"