ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Im writing currently a small script for a task. I want to convert all founded files to UTF-8. The problem is that i dont know the original charset. Sometimes it is binary, ascii or unknown:s
I've already research and found the tool recode but it doesnt do what i want. i could make a big if else if but i thought there must be an other way. "file -bi" show me that the original charset is "text/plain; charset=unknown-8bit". I have about ~200 Files that I want to convert:s
Here's my code:
Code:
#!/bin/bash
output=`find . -name "language.??.properties"`
text=" will be changed to utf-8"
for file in $output
do
charset=`file -bi $file`
if [[ $charset != *utf-8* ]]
then
echo $file /// [$charset] $text
#logic
fi
done
Thank you for your solutions. But What can i do now?:s
NevemTeve said i've to know the original charset but i don't know respectively it's not always the same and there are and also there are some files which which are uknown-8bit. What should i do with them?
Basically, if you don't know the encoding, then you don't know the encoding. You might guess by reading the files one-by-one, or you might simply assume something (say iso-8859-1).
I found out that one unknown file ISO-8859-2 is. So i tried to convert it with iconv but it doesn't convert as i want. I found out the original charset with
Code:
chardet < language.en.properties
.The file contains german umlauts like δόφ but i can't see it because of the wrong charset. Notepad++ says that the file is AINSI. And when i try to convert it the special german chars just turn to a question mark.
I will try now a hex editor. Are there also other possibilites?
If the file isn't converting properly, then you have the wrong charset. The "chardet" program uses a heuristic analysis algorithm, which is a fancy way of saying that it might be wrong. If Notepad++ says the file is ANSI, and Notepad++ can display the file correctly, than the file is probably in ANSI. iconv understands several different versions of ANSI, so just try them all until you find the right one. You can find a list of charsets by running "iconv -l".
On second thought, Notepad++ has the capability of converting files to UTF-8, so why not just do that? You'll be done with that one file, anyway.
If chardet doesn't give you a "1.0" score, then don't trust it. I've found it to be wrong more often than not. I've found uchardet to give more reliable results, but it's not perfect either. It is faster though, being binary.
In short, there's not perfectly reliable way to script something like this.
BTW, remember also that simple ascii is considered valid utf-8.
Now a couple of comments on the OP code:
Code:
output=`find . -name "language.??.properties"`
for file in $output; do
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.