It’s quite an academic task, but still useful sometimes. I’ve collected different ways to do it in a Linux terminal. Some of them work with UTF-8 characters (they toggle the case for “й”, “ё”, and so on). They will not, in general, handle special ligatures such as “ß” and “fi”.
Ways are: sed, perl, python, awk, tr, bash, dd.
Works with UTF-8 characters.
It is quite straightforward and allows adding custom rules easily. For example, I’ve added special handling for the ligatures “ß” and “fi”. I should note that the conversion SS -> ß is not correct in general. So you may want to remove it.
1$ echo ''Wie hйЮёßen Siefi тест'' | sed ''s/.*/\U&/;s/ß/SS/g;s/fi/FI/g''
2WIE HЙЮЁSSEN SIEFI ТЕСТ
3
4$ echo ''WIE HЙЮЁSSEN SIEFI ТЕСТ'' | sed ''s/ß/SS/g;s/fi/FI/g;s/.*/\L&/''
5wie hйюёssen siefi тест
Doesn’t work with UTF-8 characters.
I’m not a perl ninja; maybe there is a more efficient way. But it works.
1$ echo ''Wie hйЮёßen Siefi тест'' | perl -ne ''print uc($_)''
2WIE HйЮёßEN SIEfi тест
3
4$ echo ''WIE HЙЮЁSSEN SIEFI ТЕСТ'' | perl -ne ''print lc($_)''
5wie hЙЮЁssen siefi ТЕСТ
Doesn’t work with UTF-8 characters.
Python nowadays is sometimes said to be a replacement for perl. It cannot convert Cyrillic letters (UTF-8) either.
1$ echo ''Wie hйЮёßen Siefi тест'' | python -c "import sys; [sys.stdout.write(arg.upper()) for arg in raw_input()]; print "\n""
2WIE HйЮёßEN SIEfi тест
3
4$ echo ''WIE HЙЮЁSSEN SIEFI ТЕСТ'' | python -c "import sys; [sys.stdout.write(arg.lower()) for arg in raw_input()]; print "\n""
5wie hЙЮЁssen siefi ТЕСТ
Doesn’t work with UTF-8 characters in mawk, works with UTF-8 characters in gawk.
The default awk in Ubuntu 12.04 is mawk. To get UTF-8 support you have to install gawk and use it.
1$ echo ''Wie hйЮёßen Siefi тест'' | gawk ''{for (i=1; i<=NF; i++) printf toupper($i)" "} END {print ""}''
2WIE HЙЮЁßEN SIEfi ТЕСТ
3
4$ echo ''WIE HЙЮЁSSEN SIEFI ТЕСТ'' | gawk ''{for (i=1; i<=NF; i++) printf tolower($i)" "} END {print ""}''
5wie hйюёssen siefi тест
Doesn’t work with UTF-8 characters.
It works with the current locale. But I work in the US locale and my native language is Russian.
It is the easiest way, I believe. It also fits the purpose of tr — to translate and delete characters. It is possible to add custom rules such as “tr ‘ё’ ‘Ё’", but it caused new strange symbols to appear in the output.
1$ echo ''Wie hйЮёßen Siefi тест'' | tr ''[:lower:]'' ''[:upper:]''
2WIE HйЮёßEN SIEfi тест
3
4$ echo ''WIE HЙЮЁSSEN SIEFI ТЕСТ'' | tr ''[:upper:]'' ''[:lower:]''
5wie hЙЮЁssen siefi ТЕСТ
Doesn’t work with UTF-8 characters.
Warning! It’s a weird way to convert strings, but a good way to convert variables in bash scripts.
1$ export a=''Wie hйЮёßen Siefi тест'' ; echo ${a^^}
2WIE HйЮёßEN SIEfi тест
3
4$ export a=''WIE HЙЮЁSSEN SIEFI ТЕСТ'' ; echo ${a,,}
5wie hЙЮЁssen siefi ТЕСТ
Doesn’t work with UTF-8 characters.
The bad news is that it outputs more information than just the toggled string. Just for the collection.
1$ echo ''Wie hйЮёßen Siefi тест'' | dd conv=ucase
2WIE HйЮёßEN SIEfi тест
30+1 records in
40+1 records out
532 bytes (32 B) copied, 0.000124458 s, 257 kB/s
6
7$ echo ''WIE HЙЮЁSSEN SIEFI ТЕСТ'' | dd conv=lcase
8wie hЙЮЁssen siefi ТЕСТ
90+1 records in
100+1 records out
1131 bytes (31 B) copied, 7.913e-05 s, 392 kB/s
Doesn’t work with UTF-8 characters.
PHP can be used as a scripting language for general purposes with php-cli.
1$ echo ''Wie hйЮёßen Siefi тест'' | php -r "print strtoupper(fgets(STDIN));"
2WIE HйЮёßEN SIEfi тест
3
4$ echo ''WIE HЙЮЁSSEN SIEFI ТЕСТ'' | php -r "print strtolower(fgets(STDIN));"
5wie hЙЮЁssen siefi ТЕСТ