Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there a tool (command-line is fine) that can convert accented characters to HTML entities in Ubuntu? Preferably recursively and without also converting html/php tags.

e.g.
from: é
to: é
or: é
share|improve this question

3 Answers 3

up vote 1 down vote accepted

Recode can convert to HTML entities:

$ echo "é" | recode ..html
é

There are a few slightly different HTML transformations available in recode; see info recode HTML.

If you want to recode a file or some files, you can use

$ recode ..html one_file another_file and so on

For recursive action, use the find command, e.g.

$ find your_directory -type f -name "*.html"

The above find command will only show the files. Please make sure that you have found only the right files, not any binaries and not any files in unwanted directories. It is also a good idea to make a backup or use a copy of your files, not the real files. If you have found the correct find command, append -exec your_command {} +, where your_command is the recode ..html from above and the {} denotes the file(s) which are given by find to recode:

$ find your_directory -type f -name "*.html" -exec recode ..html {} +

But wait a moment, there's one big caveat: recode ..html assumes that your input files are in the same character set (encoding) that you are using on the command line. If all of your files use the "modern" UTF-8, it will work fine, because Ubuntu used UTF-8 from the standard. But if some of your files use the older ISO-8859-1 or other charsets, it will be a lot more complicated.

share|improve this answer
    
Ya I ran a rename command to get rid of all the spaces in the files with rename 's/\ /_/g' * and then for i in *.php; do iconv --from-code=ISO-8859-15 --to-code=UTF-8 $i > $i.iconv; mv $i.iconv $i; done to convert to UTF-8. Problem with that program is that it does every character imaginable, including html and php tags. –  bafromca May 4 '11 at 19:11
    
You didn't need to rename the files. You can use double quotes around your variable values, i.e. "$i". These double quotes make sure that your variable values are not split. –  elmicha May 5 '11 at 18:13

probably sed. Tutorial here

http://www.grymoire.com/Unix/Sed.html#uh-6

for yours example, maybe command

sed s/"é"/"\&#233"/g < oldfile > newfile

( repair me if im wrong, havent seen sed for a while )

GUI

probably default installed gedit, ctrl+H

share|improve this answer
    
I'm aware of those tools but I need to convert hundreds of files (so gedit is out) and I need to convert all accented characters (and there are a lot of those). –  bafromca May 4 '11 at 15:37
    
if you need to convert hundreds of files, you use that sed with find, maybe like this find /folder_where_you_have_files -mindepth 0 -name *.html -exec sed s/"é"/"\&#233"/g < {} > {}.new \; sed can read instructions from file, so you can replace all char at once. Im not sure that i typed command exactly right, will try on some examples and post if something changes –  Denwerko May 4 '11 at 18:13

Check out the source of http://www.text-symbols.com/tools/escape-chars/

Take that javascript, execute it in a js environment that can do some IO (node.js) and just use find -exec to find all files to convert and execute your node-based converter on all the necessary files.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.