brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: iconv converts an old Latin-1 file to UTF-8 so the accents show up right

Linux
~/tips/tip-linux-iconv-latin1-to-utf8

iconv -f latin1 -t utf-8 old.csv > new.csv

Fix the garbled characters in an old export by converting its encoding.

A CSV from an old system shows caf? or a strange box instead of café. Most of the time the file is in Latin-1 and your terminal expects UTF-8.

iconv -f latin1 -t utf-8 old.csv > new.csv

-f is the encoding the file is in now, -t the one you want. After this, the accented characters display normally in the terminal and in tools that assume UTF-8.

The tricky part is knowing the source encoding. Files from Windows are often cp1252, which is Latin-1 plus a few extra characters such as the euro sign and curly quotes. If you see odd results with latin1, I would try cp1252 next. iconv -l lists every name your system knows.

To check whether a file already is valid UTF-8, convert it from UTF-8 to UTF-8:

iconv -f utf-8 -t utf-8 old.csv > /dev/null

If it is not valid, iconv stops with illegal input sequence at position and a byte offset, and exits with a non zero code.

Do not run the conversion twice. Converting a file that is already UTF-8 from Latin-1 again turns every accent into two strange characters, and that mess is harder to undo. Keep the original until you have checked the result.

Further reading: iconv(1) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link