Bash: Sort text file by bytewise case sensitive sort command or using python sort command Bash: Sort text file by bytewise case sensitive sort command or using python sort command bash bash

Bash: Sort text file by bytewise case sensitive sort command or using python sort command


This command will do it:

LC_ALL=C sort -k 1.1f,1.1 PATH

where PATH is your file path.

Explanation:

  • sort collation order is affected by the current locale, so LC_ALL=C is used to set the locale to a known value (the POSIX locale, collation order based on ASCII character code values)
  • -k 1.1f,1.1 tells sort to use the first character as the primary sort key in a case-insensitive manner
  • Equal comparisons of the primary key will be resolved by comparing all characters again (this time, in a case-sensitive manner).

The output is exactly as requested in the question.


I managed to do it! Here it is (in Python):

import stringwords = [...]newwords = sorted(words, key=lambda x: [ord(y) if y in string.lowercase else ord(y.lower()) - 0.5 for y in x])print(newwords)

Output:

['Act', 'about', 'across', 'Bad', 'Bag', 'back', 'ball', 'Card', 'camera', 'canvas', 'danger', 'dark', 'East', 'early', 'edge']