5

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.

1

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"    
  • Hi @fd0, sort -m "$@" file.txt | tr 'a-zA-Z' 'A-Za-z' | sort file.txt works. The last command has to be deleted or all lowercase-uppercase letters are swapped. Would you mind deleting that part? Thanks for the answer! – regina_fallangi Sep 7 '18 at 7:43
  • All I understand is that the output of one command is fed into another, but I have to honest and say I was not sure where I should put the filename. I tried different combinations and the one in my first comment worked. Happy to learn where one has to set the file name in your original solution. Thanks. – regina_fallangi Sep 7 '18 at 11:41

You must log in to answer this question.

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