Sort in bash with capital words at the end

Alec Jacobson

September 12, 2013

weblog/

I wanted to do a sort of some lines in bash but instead of capitals being treated as coming before minuscules I wanted them after. Normally the sort command:

echo -e "Foo\nfoo\nBar\nbang" | sort

will produce:

Bar
Foo
bang
foo

Instead I wanted Bar and Foo to come after bang and foo. To do this I used:

echo -e "Foo\nfoo\nBar\nbang" | \
  ruby -ne 'puts $_.split("").map{|e| (e>="a"?e.upcase():e.downcase())}.join' | \
  sort | \
  ruby -ne 'puts $_.split("").map{|e| (e>="a"?e.upcase():e.downcase())}.join'

which produces

bang
foo
Bar
Foo