LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to switch charset in terminal? (https://www.linuxquestions.org/questions/linux-software-2/how-to-switch-charset-in-terminal-925945/)

Himilhil 01-27-2012 04:41 AM

How to switch charset in terminal?
 
I have to execute with a sqlplus sql scripts written using CP1251 charset. For this purpose i assigned "RUSSIAN_RUSSIA.CL8MSWIN1251" to NLS_LANG environment variable. At the same time a terminal i execute scripts on is configured to use UTF-8 charset by default. Problem is that the sqlplus outputs messages in CP1251 and they are unreadable on the terminal. I tried to assign ru_RU.CP1251 to LANG environment variable. It didn't help. Is there a short way to switch charset in one virtual terminal and then switch it back?

lithos 01-27-2012 06:07 AM

Hi,

did you try this ?
Maybe it will work

good luck

Himilhil 01-27-2012 06:58 AM

Quote:

Originally Posted by lithos (Post 4585785)
Hi,

did you try this ?
Maybe it will work

good luck

Yes, i did. It doesn't work:
Code:

$ echo $LANG
ru_RU.utf8
$ echo $LC_ALL

$ enconv -x cp1251 test.sql
$ export LANG=ru_RU.cp1251
$ export LC_ALL=$LANG
$ cat test.sql
�����


lithos 01-27-2012 11:41 AM

I noticed you are using some mysql query and as it happened to me that some CP-8859-2 characters in database were not displayed I had to use:
Code:

mysql charset connection
Posted by Charly R on March 29 2007 12:24am
I've spent quite a lot of time trying to make MySQL 4.1 and PHP working with my tables in cp1251 charset.

Adding on my local PC the following line to the my.ini solved the problem but unfortunately I can't do the same on my hosting provider space
default-character-set=cp1251

So after trying quite a lot of combinations finally I've found that I need to add only one query set just after connecting to the database in my php script
mysql_query ('SET NAMES CP1251');

It's worth reading it though it can solve the problem with mysql character sets displaying unknown characters.


good luck

Nominal Animal 01-27-2012 04:12 PM

if you are using Bash, then set
Code:

export LANG="ru_RU.CP1251" LC_ALL="ru_RU.CP1251"
to set the locale to Russian using cp1251 charset.

You can always run a single command using
Code:

command | iconv -f cp1251//TRANSLIT
to display the output of the command correctly in your terminal, regardless of whatever charset your terminal uses, if the command output is in cp1251. (The //TRANSLIT tells iconv to use nearest equivalents if the exact characters cannot be displayed.)

If you have a curses program -- a text-based interactive program -- which uses some other character set than your terminal is set to, you can use luit :
Code:

luit -encoding cp1251 -- command
to run it. Very good for old text-based games, I hear. ;)

I like to keep my terminal in UTF-8, but connect to an ancient machine which uses ISO-8859-15. A simple luit -encoding iso-8859-15 ssh ancient.machine works perfectly. If interrupted/killed, luit may forget to restore the terminal settings, but that is harmless: just run stty sane or tput reset to fix. The former will not clear the terminal, but may not work; the latter will clear your scrollback. Both are completely safe to run.

Himilhil 02-08-2012 05:55 AM

Quote:

Originally Posted by Nominal Animal (Post 4586254)
if you are using Bash, then set
Code:

export LANG="ru_RU.CP1251" LC_ALL="ru_RU.CP1251"
to set the locale to Russian using cp1251 charset.

Apparently it doesn't work. Bash still uses utf8 encoding while handling output of sqlplus after I've changed LANG and LC_ALL. When sqlplus prints out with cp1251 russian characters in it's output looks still unreadable.
Quote:

Originally Posted by Nominal Animal (Post 4586254)
You can always run a single command using
Code:

command | iconv -f cp1251//TRANSLIT
to display the output of the command correctly in your terminal, regardless of whatever charset your terminal uses, if the command output is in cp1251. (The //TRANSLIT tells iconv to use nearest equivalents if the exact characters cannot be displayed.)

Unfortunatelly it's not an appropriate way for me. I need to work with sqlplus interactively.
Quote:

Originally Posted by Nominal Animal (Post 4586254)
If you have a curses program -- a text-based interactive program -- which uses some other character set than your terminal is set to, you can use luit :
Code:

luit -encoding cp1251 -- command
to run it. Very good for old text-based games, I hear. ;)

I like to keep my terminal in UTF-8, but connect to an ancient machine which uses ISO-8859-15. A simple luit -encoding iso-8859-15 ssh ancient.machine works perfectly. If interrupted/killed, luit may forget to restore the terminal settings, but that is harmless: just run stty sane or tput reset to fix. The former will not clear the terminal, but may not work; the latter will clear your scrollback. Both are completely safe to run.

It works in some strange way. Without luit russian characters look in sqlplus' output like question marks within hexagons. With luit they look like a jumble of various letters, digits and other characters.

Himilhil 03-14-2012 04:49 PM

I've tried to run less with luit for example:
Code:

luit -encoding cp1251 less test.txt
That's what i saw in less:
Code:

j`j`ÿ-rn jhphkkhweqj`ÿ m`dohq|
Definitely it wasn't a cp1251 encoded text which is in this file. Here is my locale settings:
Code:

$ locale
LANG=ru_RU.utf8
LC_CTYPE="ru_RU.utf8"
LC_NUMERIC="ru_RU.utf8"
LC_TIME="ru_RU.utf8"
LC_COLLATE="ru_RU.utf8"
LC_MONETARY="ru_RU.utf8"
LC_MESSAGES="ru_RU.utf8"
LC_PAPER="ru_RU.utf8"
LC_NAME="ru_RU.utf8"
LC_ADDRESS="ru_RU.utf8"
LC_TELEPHONE="ru_RU.utf8"
LC_MEASUREMENT="ru_RU.utf8"
LC_IDENTIFICATION="ru_RU.utf8"
LC_ALL=

I've also checked if i have a proper locale to display a cp1251 encoded text:
Code:

$ locale -a | egrep '.*ru.*1251.*'
ru_RU.cp1251

If i try to convert content of this file with iconv -f CP1251 i have a correct cyrillic text on screen. What do I wrong?


All times are GMT -5. The time now is 04:46 AM.